python django 表单【Part4】
2013-05-12 21:34
260 查看
表单
写一个简单的表单
polls/detail.html
<h1>` poll`.`question `</h1>
{% if error_message %}<p><strong>` error_message `</strong></p>{%
endif %}
<form action="{% url ’polls:vote’ poll.id %}" 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_text `
</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
表单提交视图:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from 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(request, ’polls/detail.html’, {
’poll’: 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,)))
投票结果视图:
def results(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
return render(request, ’polls/results.html’, {’poll’: poll})
投票结果模板:polls/results.html
<h1>` poll`.`question `</h1>
<ul>
{% for choice in poll.choice_set.all %}
<li>` choice`.`choice_text ` -- ` choice`.`votes ` vote{{ choice.votes|
pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url ’polls:detail’ poll.id %}">Vote again?</a>
写一个简单的表单
polls/detail.html
<h1>` poll`.`question `</h1>
{% if error_message %}<p><strong>` error_message `</strong></p>{%
endif %}
<form action="{% url ’polls:vote’ poll.id %}" 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_text `
</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
表单提交视图:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from 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(request, ’polls/detail.html’, {
’poll’: 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,)))
投票结果视图:
def results(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
return render(request, ’polls/results.html’, {’poll’: poll})
投票结果模板:polls/results.html
<h1>` poll`.`question `</h1>
<ul>
{% for choice in poll.choice_set.all %}
<li>` choice`.`choice_text ` -- ` choice`.`votes ` vote{{ choice.votes|
pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url ’polls:detail’ poll.id %}">Vote again?</a>
相关文章推荐
- Python+Django+SAE系列教程14-----使表单更安全
- Python网络编程08----Django表单
- Python3.4 + Django1.7.7 搭建简单的表单并提交
- python django框架反向生成页面表单
- Python+Django+SAE系列教程11-----request/pose/get/表单
- 循序渐进Python3(十三) --3-- django之form表单(为自动生成的html标签添加样式)
- Python中使用django form表单验证的方法
- Python3.4 + Django1.7.7 搭建简单的表单并提交
- django python 文件上传【Part 5】
- django python 创建app及models【Part 1】
- Python中Django 后台自定义表单控件
- Part 4:表单和类视图--Django从入门到精通系列教程
- python Django框架实现自定义表单提交
- python3.4 + Django1.7.7 表单的一些问题
- 循序渐进Python3(十三) --0-- django之form表单
- Python系列视频教程: Django【13讲】第13讲 表单使用
- python django 书写视图view【Part 3】
- Python Django基础教程(五)(表单)
- python3.4 + Django1.7.7 表单的一些问题
- python自动化之djangoform表单验证