尊重作者的劳动,转载请注明作者及原文地址 http://www.cnblogs.com/txwsqk/p/6514113.html

完全翻译自官方文档 https://docs.djangoproject.com/en/1.10/intro/tutorial04/

本节内容讲表单

让我们更新一下pools/detail.html,添加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>

解释一下上面的代码:

form里添加了一个radio单选框,label是单选框的标签,每个单选框的值是 choice.id,当你点了"submit"提交表单时,实际提交的内容是

一个变量choice=choice.id 即 input的name和value,表单提交的方法必须是post

forloop.counte是for循环的一个计数器,表示循环了多少次

{% csrf_token %}  这个是用来防止跨站攻击的,django已经帮我们做好了,放在form上就可以了

提交这个表单时,匹配的url是这个

url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),

现在更新一下我们的vote的视图

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse from .models import Choice, Question
# ...
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'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=(question.id,)))

解释request.POST 你用post提交表单时传的参数就是用这个接收,是一个字典类型的;对应的request.GET就是接收get请求的参数

request.POST['choice']当key不存在时,会抛一个KeyError异常,所以代码要捕获这个异常
HttpResponseRedirect(url)当成功提交表单返回结果时就用这个函数
reverse这个函数是为了避免url的硬编码,本来这个url应该是这样的 url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
现在呢
reverse('polls:results', args=(question.id,) 看不明白的请去复习上一节的内容(urls.py里的app_name和url()里的name就是这个用处
 

现在来完成result的视图

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})

result的视图有了,下面写展示的模板

<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>

这个例子就是你选择单选按钮,然后点击提交按钮,跳转到一个新的页面,没有使用ajax.

注意:

vote视图这里有并发问题,当多个人对同一个question投票时,数据库在存和取时就有资源竞争问题了

django的解决方案在此 https://docs.djangoproject.com/en/1.10/ref/models/expressions/#avoiding-race-conditions-using-f

厉害的来了

通用视图

考虑一下我们前面写的视图detail(),results(),index()都是web开发中最通用的操作,根据url的参数从数据库存取内容,然后模板展示,这就是django通用视图考虑的问题,下面让我们用通用视图修改一下我们前面的应用pools

先修改urls.py

from django.conf.urls import url

from . import views

app_name = 'polls'
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'),
]

修改视图views.py

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.urls 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, no changes needed.

这里我们用了两个新的视图ListView和DetailView

ListView 展示成列表

DetailView 根据不同的model展示不同的详情页

这两个视图都需要一个叫model的属性,它得知道根据那个model展示相应的内容

DetailView通用视图是根据url中的的pk参数来获取数据库内容的,所以我们urls.py做了相应修改

这两个通用视图如果你不传template_name那么会使用默认的模板文件

DetailView使用<app name>/<modelname>_detail.html 本应用中就是 "polls/question_detail.html"

ListView使用<app name>/<modelname>_list.html ,当然你传了template_name它就用你定义的模板了

在前面的章节中我们可以给urls.py里定义的url()定义一个context字典来给view()传参数,通用视图有预定义的变量可以使用

DetailView使用model的小写作为context变量的key

ListView使用model的小写+_list作为context的key

当然你想使用自己的context可以传context_object_name来自定义你的context

更多通用视图的内容请参考 https://docs.djangoproject.com/en/1.10/topics/class-based-views/

本节完

django入门-表单-part4的更多相关文章

  1. python运维开发(十九)----Django后台表单验证、session、cookie、model操作

    内容目录: Django后台表单验证 CSRF加密传输 session.cookie model数据库操作 Django后台Form表单验证 Django中Form一般有2种功能: 1.用于做用户提交 ...

  2. django form表单验证

    一. django form表单验证引入 有时时候我们需要使用get,post,put等方式在前台HTML页面提交一些数据到后台处理例 ; <!DOCTYPE html> <html ...

  3. django from表单验证

    django from表单验证   实现:表单验证 工程示例: urls.py 1 2 3 4 5 6 7 8 9 from django.conf.urls import url from djan ...

  4. django Form表单的使用

    Form django表单系统中,所有的表单类都作为django.forms.Form的子类创建,包括ModelForm 关于django的表单系统,主要分两种 基于django.forms.Form ...

  5. Django(5) session登录注销、csrf及中间件自定义、django Form表单验证(非常好用)

    一.Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 数据库(默认) 缓存 文件 缓存+数据库 加密cookie 1.数据库Session 1 2 3 4 5 ...

  6. django创建表单以及表单数据类型和属性

    08.15自我总结 关于django的表单不同关系之间的创建 一.不同关系之间的创建 1.一对一 举例 母表:userinfo id name age 1 张三 12 2 李四 58 字表:priva ...

  7. Django form表单 组件

    目录 Django form表单 组件 Form 组件介绍 普通方式手写注册功能 使用form组件实现注册功能 Form 常用字段与插件 常用字段(必备) 字段参数(必备) 内置验证(必备) 自定义效 ...

  8. [转]django自定义表单提交

    原文网址:http://www.cnblogs.com/retop/p/4677148.html 注:本人使用的Django1.8.3版本进行测试 除了使用Django内置表单,有时往往我们需要自定义 ...

  9. Django实现表单验证、CSRF、cookie和session、缓存、数据库多表操作(双下划綫)

    通常验证用户输入是否合法的话,是前端js和后端共同验证的,这是因为前端js是可以被禁用的,假如被禁用了,那就没法用js实现验证合法与否了,也就是即使用户输入的不合法,但是也没提示,用户也不知道怎么输入 ...

随机推荐

  1. 《C语言基础日常笔记》

    1. 类型转换-----------------20130902 a, 浮点数(包括单精度与双精度)赋值给整型变量时,舍弃浮点数的小数部分,直接将其整数部分存放在整型变量里. b, 整型变量赋值给浮点 ...

  2. 在VMware Workstation中安装Ubuntu Server 16.04.5图解教程

    最近要在Ubuntu中部署openstack,为了节省空间和内存,最终选择了Ubuntu服务器.看了很多前辈和大佬的安装教程,在这里记录一下我自己的Ubuntu Server 16.04.5的安装过程 ...

  3. windchill 跑物料变更流程后无法发送物料到SAP

    2042000065.2042000064.2042000074.2042000066在发主数据时,流程卡住,SAP返回信息为空 核查:PLM后台日志只显示变更零件,开始,然后就结束 原因:ECR号为 ...

  4. VMware Workstation 虚拟机的服务启动项

  5. Visual Studio2017 设置了vcpkg之后,编译其他程序出问题

    博客参考:https://github.com/nodejs/node/issues/23909 错误如下 LNK2005 _SSL_CTX_check_private_key already def ...

  6. 在Linux下访问Windows共享文件夹

    说明 以下操作以Ubuntu为例,大家可以参考. 我在Ubuntu 14.04和16.04都试过了. Windows共享文件夹 如果局域网内有一台Windows主机,将指定文件夹设为共享,就可以在局域 ...

  7. .net手动编写Windows服务

    1,打开VS,新建一个windows服务程序.项目名称自定义,我这里用的默认名称:Service1 2,打开Service1,按F7查看代码.代码里有三个方法:public Service1().pr ...

  8. SQLServer函数 left()、charindex()、stuff()的使用

    1.left() LEFT (<character_expression>, <integer_expression>)   返回character_expression 左起 ...

  9. GCT英语口语复试中的常见问题总汇

    英语口语复试中常见的问题: 1. Where do you come from? 2. What kind of landscape surrounds your hometown? 3. What ...

  10. 关于Spring Data redis几种对象序列化的比较

    redis虽然提供了对list set hash等数据类型的支持,但是没有提供对POJO对象的支持,底层都是把对象序列化后再以字符串的方式存储的.因此,Spring data提供了若干个Seriali ...