前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >django2.0入门教程第四节

django2.0入门教程第四节

作者头像
章鱼喵
发布2018-06-27 15:23:41
7480
发布2018-06-27 15:23:41
举报
文章被收录于专栏:codingcoding

继上篇 django2.0入门教程第三节,介绍了django2.0的视图views和模板template, 本节介绍如何在前台进行投票。

构建一个简单的表单提交页

polls/templates/polls/detail.html

代码语言:javascript
复制
<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 id="choice{{ forloop.counter }}" type="radio" name="choice" value="{{ choice.id }}">
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
    {% endfor %}
    <br />
    <input type="submit" name="" id="" value="投票" />
</form>

代码解析:

  • form表单提交的url为{%url 'polls:vote' question.id %}, 即表示访问polls/views.py的vote方法,并携带问题id作为参数。
  • 将问题的相关选项遍历,以单选框显示
  • form表单用post方式提交数据

配置url

polls/urls.py

代码语言:javascript
复制
path('<int:question_id>/vote/', views.vote, name='vote'),

投票页面截图 http://127.0.0.1:8000/polls/1/

vote.png

视图层处理提交结果

polls/views.py

代码语言:javascript
复制
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .models import Question, Choice
# ...
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):
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "必须选择一个选项",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

代码解析:

  • request.POST['choice']接收表单页面提交的数据
  • 将投票次数加1,并更新数据库

显示投票结果

polls/views.py

代码语言:javascript
复制
from django.shortcuts import render, get_object_or_404
# ...
def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/results.html', {'question': question})

results.html

代码语言:javascript
复制
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{choice.votes}}</li>
{% endfor %}
</ul>

<a href="{% url 'polls:detail' question.id %}">再投一次?</a>

提交结果页面 http://127.0.0.1:8000/polls/1/results/

result.png

优化url和view写法

另一种写法:

将主键id代替question_id

polls/urls.py

代码语言:javascript
复制
#_*_coding:utf8_*_
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

使用<pk>代替<question_id>会更加灵活,<pd>代表主键

相应的视图也需要修改成另一种写法,vote方法保持原样,用于比较两种写法的不同

polls/views.py

代码语言:javascript
复制
#_*_coding:utf8_*_
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic 
from .models import Question, Choice

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        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):
    # ...

入门教程不会对代码进入深入的讲解,先大致了解其作用即可,后续再逐个模块进行解析

源码下载

相关源码包

如果对django2.0教程感兴趣,请关注我的简书,持续更新中...

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 构建一个简单的表单提交页
  • 配置url
  • 视图层处理提交结果
  • 显示投票结果
  • 优化url和view写法
  • 源码下载
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档