知识点

  1) 表单的编写

    CSRF问题

    forloop.counter

  2) 视图函数的知识

    GET和POST

    HttpResponseRedirect的使用

    reverse的使用

  3) 通用视图

    CBV(class_base view)

    CVB在URLs的配置, 使用as_view()

    ListView和DetailView

    注意主键获取, 名字用fk

    默认模板的问题

    自带变量问题

1 编写表单

  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>

  表单中的第一个input, type为radio表示单选框, name为choice表示提交的时候该input的value会提交成 choice=值 的形式

  form的属性添加了action表示提交地址, method设置为post非常适合用于表单提交数据

  forloop.counter表示for循环的次数, 类似于我们常用的i

  由于提交的表单会修改数据, 这有可能造成CSRF(Cross Site Request Forgeries, 跨站点请求伪造)

  为了防范这类问题, 针对修改内部网站的所有PSOT表达都应该加上{% csrf_token %}

  URLs配置如下

  polls/urls.py

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

  视图函数编写为

  polls/views.py

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()
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

  request可以得到GET和POST两个值, 得到的结果是以个类似于字典的值, 可以用 request.GET['名字'] 或者 request.POST['名字'] 的方式来获取值

  返回使用的是HttpResponseRedirect 而不是常用的HttpResponse, 其只需传入一个参数, 是用于重定向的地址, 类型是字符串

  一般POST成功处理之后都应该使用HttpResponseRedirect, 原因是HttpResponseRedirect可以防止用户在点击回退按钮的时候重复提交表单

  可以使用reverse函数来避免硬编码(hardcode )的URL, reverse()函数可以传入两个参数, 一个是视图名, 一个是URL的参数

    如果question_id为3 那么返回的URL为

'/polls/3/results/'

  现在编写results的视图

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

  再创建results的模板

  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>

  注意:

    此次的投票系统可能出现一些小BUG

    两个使用者可能在同一时刻提交同一个选项, 该选项的计数可能不正确

    可以参见此来解决问题

2 编写通用视图

  通过编写通用的视图, 可以复用代码, 进而减少代码量

  对于通用视图, Django提供了一个快捷方式, 叫做 通用视图系统( "generic views" system)  

  具体步骤为

    1) 转换URLconf

    2) 删除一些旧的,不需要的视图

    3) 根据Django的通用视图编写新视图

  修改URL配置
  polls/urls.py

from django.conf.urls import url
from polls 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'),
]

    - 注意第二个第三个URL的名字改为了pk

  修改视图

  polls/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):
# 不做修改

  在这当中用到了ListViewDetailView两个通用视图, 分别用于显示

    对象列表

    特定对象的详细信息

  注意两点

    1) 必须要设置model, 要为其指定一个表(这里需要用类)

    2) 在捕获URL的时候, 需要获得主键信息, 这个时候就需要把正则表达式分组命名为pk, 这样命名Django才能正常识别

  默认情况下, DetailView返回的模板名也是有格式的(ListView同理), 格式为

<app name>/<model name>_detail.html
<app name>/<model name>_list.html

  另外还需要说明的是, 默认会提供一个上下文变量question 和一个全局变量latest_question_list

  对于DetailView, 使用模型Question会自动提供变量question

  对于ListView, 使用模型Question会自动提供变量question_list

  如果要重写这个变量, 可以通过context_object_name属性来完成, index()函数就修改成了last_question_list

context_object_name = 'latest_question_list'

  如果需要更多内容, 可以点击了解更多的通用视图

  

最新文章

  1. ListIterator接口,双向输出。
  2. iOS学习05C语言函数
  3. 用CAKeyframeAnimation构建动画路径
  4. map的详细用法
  5. CMake实践(1)
  6. Docker集群实验环境布署--swarm【4 管理组件--manager】
  7. AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖-转
  8. 第四弹:overfeat
  9. Spring Boot 2.X 如何优雅的解决跨域问题?
  10. golang与python多线程的并发速度
  11. Intellij idea使用过程中遇到的一些问题
  12. AngularJS基于MVC的复杂操作案例
  13. 转载--关于hdfs
  14. HBase源码分析之WAL
  15. POJ1180 Batch Scheduling -斜率优化DP
  16. Weixin 之 微信二维码扫描下载 Apk
  17. Is &quot;UNION ALL&quot; Always Better Than &quot;UNION&quot;? Watch Out!
  18. Kafka Consumer接口
  19. 流程设计器jQuery + svg/vml(Demo4 - 画连线)
  20. c#,条码

热门文章

  1. resin 4.0 项目的配置
  2. Solr6.5与mysql集成建立索引
  3. 【BZOJ2466】[中山市选2009]树 树形DP
  4. The hidden layers are either convolutional, pooling or fully connected.
  5. IO多路复用的作用?并列举实现机制以及区别?
  6. go语言之并发编程 channel
  7. FTP开启被动连接模式
  8. [转载]设计模式的UML图
  9. spring mvc入门教程 转载自【http://elf8848.iteye.com/blog/875830】
  10. 如何让Jackson JSON生成的数据包含的中文以unicode方式编码