为了增加程序的友好和健壮性,修改view代码,处理以下如果出现404,页面的UI展示。

修改view代码

from django.http import Http404
from django.shortcuts import render

from .models import Question
# ...
def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request, 'polls/detail.html', {'question': question})  

如果发生了页面404的报错,页面会提示“Question does not exist”,否则,跳转到detail.html页面

编辑detail.html如下:

{{ question }}

  

这里django还有一种便捷的处理404的办法,编辑views如下

from django.shortcuts import get_object_or_404, render

from .models import Question
# ...
def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})  

优化detail.html页面代码,引入choice的选项内容

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

  

我们在question的模板中,写入的语句如下,

<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

其中对于父链接 /polls/的引用,是写死的,如果polls某一天修改为/pollsAndVote,我们就需要跑去修改对应的模板,产生了很多工作量。这里降耦合,将html的url的部分,改为引用。

<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

 以上代码的{%url 'detail' question.id%},是从urlConf里面,读取detail的url(即配置当中的 /polls/+'<int:question_id>',后面的question_id将数值传入int),之前urlConf是这么配置的:

# the 'name' value as called by the {% url %} template tag
path('<int:question_id>/', views.detail, name='detail'),  

通过以上操作,如果我们修改detail的链接,就不需要同时修改urlConf和对应模板的html,而只需要修改urlConf。

比如您想将polls detail view的URL更改为其他内容,比如polls/specifics/12/,只需要修改polls/urls.py

...
# added the word 'specifics'
path('specifics/<int:question_id>/', views.detail, name='detail'),
...

  

在detail.html中,我们调用的是polls的url中的“detail"这个名字的url,实际项目中,可能有其他的叫blog的url也有一个detail这个名字。为了将模板和app对应起来,可以通过命名 app_name来一一对应

这个地方不知道解释得对不对,没有做过复杂得项目,不太理解为什么要用这个,原文如下:

The tutorial project has just one app, polls. In real Django projects, there might be five, ten, twenty apps or more. 

How does Django differentiate the URL names between them?For example, the polls app has a detail view, and so might an app on the same project that is for a blog. How does one make it so that Django knows which app view to create for a url when using the {% url %} template tag?

然后就提出了使用app_name的解决方案。

from django.urls import path

from . import views

app_name = 'polls'
urlpatterns = [
    path('', views.index, name='index'),
    path('<int:question_id>/', views.detail, name='detail'),
    path('<int:question_id>/results/', views.results, name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

  

更改detail.html

<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

  

最近在写django的网页代码,刚好用到这里。

优化前的框架: 编辑mysite的urls,将polls的各个views加进去,如果polls里面叫views,那新建一个product的app,里面的views就要改名;如果polls里面的有好几个页面都叫 manage.html,那就要给各种html加前缀,这里可以通过给templates里面增加文件夹解决。

django系列7:修改404页面展示,优化模板,降低urlconf和模板之间的耦合,命名app将模板和app绑定的更多相关文章

  1. Django学习路36_函数参数 反向解析 修改404 页面

    在 templates 中创建对应文件名的 html 文件 (.html) 注: 开发者服务器发生变更是因为 python 代码发生变化 如果 html 文件发生变化,服务器不会进行重启 需要自己手动 ...

  2. 学习ASP.NET Core Razor 编程系列七——修改列表页面

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  3. 针对ecshop错误404页面的优化

    在ecshop系统当中,比如你随意将商品详细页面的地址中的ID修改为一个不存在的商品ID,ecshop会自动跳转到首页.ecshop在这方面做得非常的差,甚至导致了很多的站不被搜索引擎收录.最模板提供 ...

  4. 夺命雷公狗---DEDECMS----23dedecms修改内容页面展示的信息

    我们在网站上不管点击那个影视作品的A连接都是进入到一个同样的页面,因为他们是一个模版文件: 我们还没有对这个模版进行任何的修改,所以我们要在内容模版增加标签取出对应的影视作品,而且导航条也是按照模版上 ...

  5. 学习ASP.NET Core Razor 编程系列十二——在页面中增加校验

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  6. django系列8:优化vote页面,使用通用视图降低代码冗余

    修改detail.html,将它变为一个可用的投票页面 <h1>{{ question.question_text }}</h1> {% if error_message %} ...

  7. 潭州课堂25班:Ph201805201 django框架 第十三课 自定义404页面,auth系统中的User模型,auth系统权限管理 (课堂笔记)

    当 DEBUG=True 时,django 内部的404报错信息, 自带的报错信息, 要自定义404信息,要先把 DEBUG=False , 之后要自定义4040页面,有两种方法, 方法1,在创建40 ...

  8. Django如何自定义漂亮的404页面

    目录 在templates 中添加404.html 修改settings.py 在templates 中添加404.html <!DOCTYPE html PUBLIC "-//W3C ...

  9. 通过修改 Apache 的配置文件 htaccess 文件实现自定义404页面

    最近在学习使用Apache服务器的配置,做一个记录. Apache下有个.htaccess文件,是Apache的一个特殊的配置文件.这个配置文件默认是没有的,要手动在各自的项目的根目录编写才行. 要实 ...

随机推荐

  1. leaflet动态路径

    在leaflet中使用动态路径需要结合插件使用,对比了好几个插件,最终找到leaflet.motion比较合适: leaflet地址:https://leafletjs.com/ leaflet.mo ...

  2. python2.7生成exe可执行文件

    1.安装对应python版本的py2exe py2exe下载地址 2.假设你要生成test.py脚本的exe文件 新建一个setup.py,在里面输入如下代码 #!/usr/bin/python fr ...

  3. Vuex初级入门及简单案例

    1.为什么要使用Vuex? (1)方便所有组件共享信息,方便不同组件共享信息. (2)某个组件需要修改状态和需求.   2.状态有哪些? (1)组件内部定义的data状态(通过组件内部修改) (2)组 ...

  4. linux-python3.8安装

    环境:  centos7.5 版本:python3.8 1.依赖包安装 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-deve ...

  5. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  6. App遍历探讨(含源代码)

    好像好久没有更新博客了,之前写的几篇博客关于自动化的框架的居多,其中好多博友向我提了好多问题,我没有回复.这里给博友道个歉~ ~ 总结几点原因如下: 1.我一般很少上博客,看到了都是好几天之前的问题 ...

  7. printf 函数原型

    typedef char *va_list; #define _AUPBND (sizeof (acpi_native_int) - 1) #define _ADNBND (sizeof (acpi_ ...

  8. centos7内网源站建设

    centos7内网源站建设 1.部署环境: 系统:Centos7 x86_64 应用服务:nginx.createrepo.reposync 镜像源:https://mirrors.aliyun.co ...

  9. Golang 入门 : 数组

    数组是指一系列同一类型数据的集合.数组中包含的每个数据被称为数组元素(element),这种类型可以是任意的原始类型,比如 int.string 等,也可以是用户自定义的类型.一个数组包含的元素个数被 ...

  10. 分享Winform datagridview 动态生成中文HeaderText

    缘起 很久以前给datagridview绑定列的时候都是手动的,记得以前用Display自定义属性来动态给datagridview绑定列.后来发现不行,于是还在博问发了问题: 后来热心网友帮我回答了这 ...