博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django第一个app,4
阅读量:7008 次
发布时间:2019-06-28

本文共 1732 字,大约阅读时间需要 5 分钟。

hot3.png

1,写detail 表单提交

{
{ question.question_text }}

{% if error_message %}

{

{ error_message }}

{% endif %}
{% csrf_token %}{% for choice in question.choice_set.all %}
{% endfor %}

改detail.html

{
{ question.question_text }}

    {% for choice in question.choice_set.all %}
  • {
    { choice.choice_text }} -- {
    { choice.votes }} vote{
    { choice.votes|pluralize }}
  • {% endfor %}
Vote again?

 

使用通用视图

from django.urls import pathfrom . import viewsapp_name = 'polls'urlpatterns = [    path('', views.IndexView.as_view(), name='index'),    path('
/', views.DetailView.as_view(), name='detail'), path('
/results/', views.ResultsView.as_view(), name='results'), path('
/vote/', views.vote, name='vote'),]

 

polls/views.py

from django.shortcuts import get_object_or_404, renderfrom django.http import HttpResponseRedirectfrom django.urls import reversefrom django.views import genericfrom .models import Choice, Questionclass 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.

有分两种视图

  and . 

文档有问题:

当没有使用通用视图的是,在模板中使用url 的时候不需要带app的名称,使用视图之后,都需要带上名称。这样一来 也没有省多少代码,不过多app使用模板url 的时候应该是会清晰一些

 

 

转载于:https://my.oschina.net/u/2367514/blog/1609816

你可能感兴趣的文章
程序员,请不要抢系统管理员的饭碗
查看>>
VCS双机由于ID冲突导致启动失败
查看>>
Windows 8上安装本地回环网卡
查看>>
修改计算机名的注意事项
查看>>
PowerBI从Exchange跟踪日志中分析数据和KPI展现
查看>>
你能成为什么样的人,取决于你心中的那颗种子!
查看>>
接口自动化测试系列之PHPUnit介绍和环境搭建
查看>>
通过ssl调用远程WebService
查看>>
SQL Server 何时将“脏页”回写到硬盘
查看>>
笔记本电脑的选购之一(2011年10月)
查看>>
电子商务时代必知的PKI及HTTPS
查看>>
程序员教你如何追女生
查看>>
各种测试用例简要模板
查看>>
SCCM 2007 R2部署、操作详解系列之概念篇一:SCCM功能详解
查看>>
Hyper-V 2016 系列教程34 在局域网内架设Windows时间服务器
查看>>
初级运维工程师面试题总结
查看>>
【COCOS2D-X 备注篇】cocos2dx 获取手机截屏等意外取消触屏事件的处理方法!
查看>>
“可穿戴操作系统”,期待吗?
查看>>
买《Python从小白到大牛》专题视频课程,送配套纸质图书
查看>>
Windows Server 2012 R2 WSUS-5:组策略配置自动更新
查看>>