【Django tutorial 5】
来源:互联网 发布:js iPhone 日历 编辑:程序博客网 时间:2024/06/05 18:01
15. 创建个简单的表单
按照如下内容更新polls/detail.html
<h1>{{ poll.question }}</h1>{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}<form action="/polls/{{ poll.id }}/vote/" method="post">{% csrf_token %}{% for choice in poll.choice_set.all %} <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /> <label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br />{% endfor %}<input type="submit" value="Vote" /></form>
根据这个需要对view中的detail做个小的调整
from django.template import RequestContext# ...def detail(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) return render_to_response('polls/detail.html', {'poll': p}, context_instance=RequestContext(request))
from django.shortcuts import get_object_or_404, render_to_responsefrom django.http import HttpResponseRedirect, HttpResponsefrom django.core.urlresolvers import reversefrom django.template import RequestContextfrom polls.models import Choice, Poll# ...def vote(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) try: selected_choice = p.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): # Redisplay the poll voting form. return render_to_response('polls/detail.html', { 'poll': p, 'error_message': "You didn't select a choice.", }, context_instance=RequestContext(request)) 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.views.results', args=(p.id,)))
def results(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) return render_to_response('polls/results.html', {'poll': p})同时,创建一个results.html
<h1>{{ poll.question }}</h1><ul>{% for choice in poll.choice_set.all %} <li>{{ choice.choice }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>{% endfor %}</ul><a href="/polls/{{ poll.id }}/">Vote again?</a>尝试一下访问 http://127.0.0.1/polls/ 吧
- 【Django tutorial 5】
- 【Django tutorial 2】
- 【Django tutorial 3】
- django tutorial 笔记
- Django Tutorial Part1
- Django Tutorial Part2
- Django Tutorial Part3
- Django Tutorial Part4
- django tutorial part1-part6学习
- Tutorial: Using AngularJS with Django
- django tutorial 简单应用(1)
- django 1.7tutorial 简单应用3
- tutorial 5
- 关于自动化测试(From Django Tutorial - Getting Started)
- Django tutorial(1)【翻译】编写第一个Django app,第一部分——创建项目
- Django学习记录之Django 1.8 教程(我只是官网的搬运工)Tutorial Part 1
- Django学习记录之Django 1.8 教程(我只是官网的搬运工)Tutorial Part 2
- Tutorial 5: Section Table
- javax.naming.NoInitialContextException: Need to specify class name in environment or system property
- 简单js正则表达式
- ACM HDOJ 下沙的沙子有几粒?
- variable for statis
- 'WTF Mobile Web':发掘糟糕的移动Web设计
- 【Django tutorial 5】
- Linux查找命令大全
- JavaScript框架设计
- ReadWriteWeb评出2011十大Web产品:Chrome、Evernote、Siri上榜
- android 焦点问题
- 疑难杂症
- 服务器IP出不去
- PowerDesigner怎样才能在修改表的字段Name的时候Code不自动跟着变
- Qt label 中输出变量