首页 / GO / Django(part4)
Django(part4)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Django(part4),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3006字,纯文字阅读大概需要5分钟。
内容图文
-
一个简单的form表单:
# 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> - forloop.counter:表示for循环执行的次数
- action="{% url ‘polls:vote‘ question.id %}":指定处理post 数据的url
- {% csrf_token % }:用于防止csrf攻击的tag,所有post的form都应该使用
-
处理post的代码:
# polls/urls.py url(r‘^(?P<question_id>\d+)/vote/$‘, views.vote, name=‘vote‘), #polls/views.pyfrom 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, 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,)))
# polls/view.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})
- request.POST:用于获取表单的值,同样的属性还有request.GET
- request.POST[‘choice’]:choice是key值,不存在时引发KeyError exception
- HttpResponseRedirect():参数是一个重定向的url\
- reverse():返回一个url,通过使用url name避免hardcode
-
Generic view:
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 polls.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 告诉django自动生成的template的name #如果不指定默认为<app name>/<model name>_detail.html template_name = ‘polls/detail.html‘#polls/urls.py #注意必须用<pk>指定匹配的组名 urlpatterns = patterns(‘‘, url(r‘^$‘, views.IndexView.as_view(), name=‘index‘), url(r‘^(?P<pk>\d+)/$‘, views.DetailView.as_view(), name=‘detail‘), )
原文:http://www.cnblogs.com/phenixyu/p/3990937.html
内容总结
以上是互联网集市为您收集整理的Django(part4)全部内容,希望文章能够帮你解决Django(part4)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。