快速入门Django(官网流程)3

快速入门Django(官网流程)3

继续使用web poll APP,学会创建公共界面 - “视图”。

本文是入门级操作,可以看作是官网的翻译版本,可直接照我的流程操作,
附上官网:
https://www.djangoproject.com/start/

概述

视图是Django应用程序中Web页面的“类型”,通常用于特定功能并具有特定模板。

Question “index” page – displays the latest few questions.
Question “detail” page – displays a question text, with no results but with a form to vote.
Question “results” page – displays results for a particular question.
Vote action – handles voting for a particular choice in a particular question.

在Django中,网页和其他内容由视图提供。
每个视图都由一个简单的Python函数(或基于类的视图的方法)表示。
Django将通过检查所请求的URL(确切地说,是域名后面的URL部分)来选择视图。

本教程提供了使用URLconf的基本说明,您可以参考URL调度程序以获取更多信息。

写更多视图

add a few more views to polls/views.py.

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
response = “You’re looking at the results of question %s.”
return HttpResponse(response % question_id)

def vote(request, question_id):
return HttpResponse(“You’re voting on question %s.” % question_id)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

new views into the polls.urls module by adding the following path()

from django.urls import path

from . import views

urlpatterns = [
# ex: /polls/
path(’’, views.index, name=‘index’),
# ex: /polls/5/
path(’<int:question_id>/’, views.detail, name=‘detail’),
# ex: /polls/5/results/
path(’<int:question_id>/results/’, views.results, name=‘results’),
# ex: /polls/5/vote/
path(’<int:question_id>/vote/’, views.vote, name=‘vote’),
]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在浏览器中查看“/ polls / 34 /”。它将运行该detail() 方法并显示您在URL中提供的任何ID。
尝试“/ polls / 34 / results /”和“/ polls / 34 / vote /” - 这些将显示占位符结果和投票页面。

写出实际做某事的视图

每个视图负责执行以下两项操作之一:
返回HttpResponse包含所请求页面内容的 对象,或者引发诸如此类的异常Http404。

您的视图可以读取数据库中的记录。它可以使用模板系统,如Django的 - 或第三方Python模板系统 。
它可以生成PDF文件,输出XML,动态创建ZIP文件,任何你想要的东西,使用你想要的任何Python库。

All Django wants is that HttpResponse.

让我们使用Django自己的数据库API。
这是一个新index() 视图,根据发布日期显示系统中最新的5个民意调查问题,以逗号分隔:

如果要更改页面的外观,则必须编辑此Python代码。因此,让我们使用Django’s template system, to separate the design from Python by creating a template that the view can use.

First, create a directory called templates in your polls directory.

打开anaconda prompt

django-admin startapp template

 
 
  • 1

没懂!!!!!!!!!!!!!!!
不清楚为什么要这样搞,而且出来了也是404

OK,问题解决,以下是我的bebug办法,
使用语句查看文件夹的列表,tree /f myapp,下面的tree是正确的。

(base) d:\project\diagnosis\django\mysite>tree /f myapp
卷 本地磁盘 的文件夹 PATH 列表
卷序列号为 BAC6-1686
D:\PROJECT\DIAGNOSIS\DJANGO\MYSITE\MYAPP
│  admin.py
│  apps.py
│  models.py
│  tests.py
│  views.py
│  __init__.py
│
├─migrations
│  │  __init__.py
│  │
│  └─__pycache__
│          __init__.cpython-36.pyc
│
├─templates
│      index.html
│
└─__pycache__
        admin.cpython-36.pyc
        models.cpython-36.pyc
        views.cpython-36.pyc
        __init__.cpython-36.pyc

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

templates不要按照官网的写,这样写就可以了。

官网说这么写
In other words, your template should be at polls/templates/polls/index.html. Because of how the app_directories template loader works as described above, you can refer to this template within Django simply as polls/index.html.
调了好几次都不好,恶心,哼!

OK
下面略,详见官网。

快捷方式:render()

引发404错误

快捷方式:get_object_or_404()

使用模板系统

删除模板中的硬编码URL

命名空间URL名称

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/qq1376725255">
                <img src="https://profile.csdnimg.cn/1/1/9/3_qq1376725255" class="avatar_pic" username="qq1376725255">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/2x/5.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/qq1376725255" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">帅金毛</a></span>
                                        </div>
                <div class="text"><span>发布了82 篇原创文章</span> · <span>获赞 13</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=qq1376725255" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>
</article>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值