• django入门-表单-part4


    尊重作者的劳动,转载请注明作者及原文地址 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/

    本节完

  • 相关阅读:
    Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
    django migrate无效的解决方法
    KindEditor echarts
    用Django实现Video页面分类查询
    pycharm快捷键及一些常用设置
    mysql 远程访问权限
    hibernate5.2需要的最少jar文件
    Struts2.5需要的最少jar文件
    修改MySQL默认字符集编码
    Android快乐贪吃蛇游戏实战项目开发教程-06虚拟方向键(五)绘制方向键箭头
  • 原文地址:https://www.cnblogs.com/txwsqk/p/6514113.html
Copyright © 2020-2023  润新知