LOADING

Follow me

Django Form
十二月 1, 2013|IT

Django Form


  1. Django 1.5.4 专题八 Model
  2. 一.修改article/forms.py内容如下
  3. from django import forms
  4. from models import Article

  5. class ArticleForm(forms.ModelForm):

  6.     class Meta:
  7.         model=Article
  8.         fields=(‘title’,’body’,’pub_date’)
  9. 二.修改article/urls.py
  10.  from django.conf.urls import patterns,include,url

 urlpatterns = patterns('',
  1. url(r'^all/$','article.views.articles'),
  2. url(r'^get/(?P<article_id>\d+)/$','article.views.article'),
  3. url(r'^language/(?P<language>[a-z\-]+)/$','article.views.language'),
  4. url(r'^create/$','article.views.create'),
  5. )
  6. 三.修改article/views.py内容如下 
  7. from django.http import HttpResponse
  8. from django.shortcuts import render_to_response
  9. from article.models import Article
  10. from forms import ArticleForm
  11. from django.http import HttpResponseRedirect
  12. from django.core.context_processors import csrf
  13. def hello(request):
  14. name ="Mike"
  15. html =" <html> <body> Hi %s,this seems to have worked! </body> </html> " % name
  16. return HttpResponse(html)
  17. def hello_template_simple(request):
  18. name ="Mike"
  19. return render_to_response('hello.html',{'name':name})
  20. def articles(request):
  21. language ='en-gb'
  22. session_language ='en-gb'
  23. if 'lang' in request.COOKIES:
  24. language = request.COOKIES['lang']
  25. if 'lang' in request.session:
  26. session_language =request.session['lang']
  27. return render_to_response('articles.html',
  28. {'articles':Article.objects.all(),'language':language,'session_language':session_language})
  29. def article(request,article_id=1):
  30. return render_to_response('article.html',
  31. {'article':Article.objects.get(id=article_id)})
  32. def language(request,language='en-gb'):
  33. response =HttpResponse("setting language to %s" % language)
  34. response.set_cookie('lang',language)
  35. request.session['lang']=language
  36. return response
  37. def create(request):
  38. if request.POST:
  39. form=ArticleForm(request.POST)
  40. if form.is_valid():
  41. form.save()
  42. return HttpResponseRedirect('/articles/all')
  43. else:
  44. form=ArticleForm()
  45. args={}
  46. args.update(csrf(request))
  47. args['form']=form
  48. return render_to_response('create_article.html',args)
  49. 四.修改article/templates/create_article.html内容如下
  50. form 可以使用as_p() as_ul() as_table()这些方法
  51. {% extends "base.html" %}
  52. {% block sidebar %}
  53. <ul>
  54. <li><a href="/articles/all">Cancel</a></li>
  55. </ul>
  56. {% endblock %}
  57. {% block content %}
  58. <form action="/articles/create/" method="post">{% csrf_token %}
  59. <ul> {{ form.as_ul}} </ul>
  60. <input type="submit" name="submit" value="Create Article">
  61. </form>
  62. {% endblock %}
  63. 五.修改article/templates/articles.html内容如下
  64. {% extends "base.html" %}
  65. {% block sidebar %}
  66. <ul>
  67. <li><a href="/articles/all">Articles</a></li>
  68. <li><a href="/articles/create">Create Article</a></li>
  69. </ul>
  70. {% endblock %}
  71. {% block content %}
  72. <h2>Language is :{{ language }}</h2>
  73. <h2>Session Language is : {{ session_language }}</h2>
  74. {% if articles.count > 0 %}
  75. {% for article in articles %}
  76. <div>
  77. <h2><a href="/articles/get/{{ article.id }}/">{{ article.title }}</a></h2>
  78. <p> {{ article.body|lower|truncatewords:"10" }}</p>
  79. </div>
  80. {% endfor %}
  81. {% else %}
  82. <p>None to show!</p>
  83. {% endif %}
  84. {% endblock %}
  85. 最终效果如图


版权声明:本文为博主原创文章,未经博主允许不得转载。

no comments
Share

发表评论