为了增加程序的友好和健壮性,修改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. C#开发WEBService服务 C++开发客户端调用WEBService服务

    编写WEBService服务端应用程序并部署 http://blog.csdn.net/u011835515/article/details/47615425 编写调用WEBService的C++客户 ...

  2. net view 提示6118错误 解决方法。

    1.win+R ,输入services.msc 开启服务:Server ,WorkStation,computer Browser 2.如果你的电脑没有computer Browser服务,win+R ...

  3. Left Jion等价SQL猜想验证

    猜想:以下两条SQL等价 select * from A left join B on A.ID=B.BID and B.BName=N'小明' select * from A left join ( ...

  4. Linux分页机制之分页机制的实现详解--Linux内存管理(八)

    1 linux的分页机制 1.1 四级分页机制 前面我们提到Linux内核仅使用了较少的分段机制,但是却对分页机制的依赖性很强,其使用一种适合32位和64位结构的通用分页模型,该模型使用四级分页机制, ...

  5. kubernetes 集群安装etcd集群,带证书

    install etcd 准备证书 https://www.kubernetes.org.cn/3096.html 在master1需要安装CFSSL工具,这将会用来建立 TLS certificat ...

  6. tmpfs使用探讨

    一. 什么是tmpfs? tmpfs是一种基于内存的文件系统,它和虚拟磁盘ramdisk比较类似,但不完全相同,和ramdisk一样,tmpfs可以使用RAM,但它也可以使用swap分区来存储. 而且 ...

  7. Loj #2321. 「清华集训 2017」无限之环

    Loj #2321. 「清华集训 2017」无限之环 曾经有一款流行的游戏,叫做 *Infinity Loop***,先来简单的介绍一下这个游戏: 游戏在一个 \(n \times m\) 的网格状棋 ...

  8. Docker(1):CentOS7 安装Docker

    1.查看系统内核,docker要求系统的内核版本高于3.10 #  uname -r 2.升级yum包,确保最新 #   yum update 3.安装所需要依赖包 #   yum install - ...

  9. PHP奇淫技巧

    https://www.jb51.net/list/list_67_1.htm PHP技巧:https://www.jb51.net/list/list_67_13.htm mysql三范式 1NF: ...

  10. 【Consul】CONSUL调研

    [Consul]CONSUL调研 2016年08月18日 18:31:53 YoungerChina 阅读数:1962更多 所属专栏: Consul修炼   版权声明:原创不易,转载请注明出处! ht ...