django学习笔记(4)
2015-04-28 16:03
323 查看
Part 4: Forms and generic views
[b]====> Write a simple form[/b]
$ edit polls\templates\polls\detail.html
$ edit polls\views.py
$ edit polls\views.py
$ edit polls\templates\polls\results.html
[b]====> Use generic views: Less code is better[/b]
$ edit polls\urls.py
$ edit polls\views.py
[b]====> Write a simple form[/b]
$ edit polls\templates\polls\detail.html
<h1>{{ question.question_text }}</h1> {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} <form action="{% url 'polls:vote' question.id %}" method="post"> {% csrf_token %} {% for choice in question.choice_set.all %} <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /> <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br /> {% endfor %} <input type="submit" value="Vote" /> </form>
$ edit polls\views.py
from django.shortcuts import get_object_or_404, render from django.http import HttpResponseRedirect, HttpResponse from django.core.urlresolvers import reverse from .models import Choice, Question # ... def vote(request, question_id): p = get_object_or_404(Question, pk=question_id) try: selected_choice = p.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): # Redisplay the question voting form. return render(request, 'polls/detail.html', { 'question': p, 'error_message': "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
$ edit polls\views.py
from django.shortcuts import get_object_or_404, render def results(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/results.html', {'question': question})
$ edit polls\templates\polls\results.html
<h1>{{ question.question_text }}</h1> <ul> {% for choice in question.choice_set.all %} <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li> {% endfor %} </ul> <a href="{% url 'polls:detail' question.id %}">Vote again?</a>
[b]====> Use generic views: Less code is better[/b]
$ edit polls\urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.IndexView.as_view(), name='index'), url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'), url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'), url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'), ]
$ edit polls\views.py
from django.shortcuts import get_object_or_404, render from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse from django.views import generic from .models import Choice, Question class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_question_list' def get_queryset(self): """Return the last five published questions.""" return Question.objects.order_by('-pub_date')[:5] class DetailView(generic.DetailView): model = Question template_name = 'polls/detail.html' class ResultsView(generic.DetailView): model = Question template_name = 'polls/results.html' def vote(request, question_id): ... # same as above
相关文章推荐
- django_book学习笔记4-模型
- 【django 学习笔记】03-模板
- 【django 学习笔记】16-中间件
- Django学习笔记1
- Django学习笔记
- Django框架学习笔记(3.实现用户登录(下))
- Django学习笔记-MySQL
- Django学习笔记
- Django学习第一天学习笔记-环境搭建、模版变量
- Django 学习笔记(七)数据库基本操作(增查改删)
- Django 学习笔记(一)
- Django学习笔记2
- Django 1.7 初级教程or学习笔记 ------- 后记
- Django学习笔记之博客开发(一)
- 20121022 django学习笔记1
- Django框架学习笔记(20.Cookies简易实例)
- python Django 学习笔记(四)—— 使用MySQL数据库
- python Django 学习笔记(六)—— 写一个简单blog做增删改练手
- Django学习笔记 大洋狗!创建工程第一步
- Django学习笔记--第一天--搭建一个简易博客