为了增加程序的友好和健壮性,修改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. t-sql语句创建表(基础)

    create table ta1 (     id int identity(1,2) not null,     name nvarchar(20) not null,     identify v ...

  2. table 的宽度设置无效

    1.在table 标签添加样式 table-layout: fixed; 必须设置width的值:<table style="table-layout: fixed"> ...

  3. Zookeeper与Kafka基础概念和原理

    1.zookeeper概念介绍 在介绍ZooKeeper之前,先来介绍一下分布式协调技术,所谓分布式协调技术主要是用来解决分布式环境当中多个进程之间的同步控制,让他们有序的去访问某种共享资源,防止造成 ...

  4. Redis内存优化memory-optimization

    https://redis.io/topics/memory-optimization  官方文档 一.特殊编码: 自从Redis 2.2之后,很多数据类型都可以通过特殊编码的方式来进行存储空间的优化 ...

  5. Python标准库之ConfigParser模块

    配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 option: b) section 用 [sect_name] 表示,每个option是一个键值对, ...

  6. SpringBoot四大神器之Actuator

    介绍 Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供 ...

  7. 贷款资讯类APP、贷款资讯网站廉价卖,需要的进来看看

    [app介绍]卡贷资讯app为您提供信用卡申请攻略及借款资讯以及贷款口子,让你借钱借款路上不再愁.[功能特点]1.资讯:聚合各种贷款资讯知识,掌握核心信用卡申请攻略,借款借钱不亏,亦不被骗:2.工具: ...

  8. (十一)Updating Documents

    In addition to being able to index and replace documents, we can also update documents. Note though ...

  9. 百度杯”CTF比赛 九月场 123

    进去后让登录,先看源码有提示 进到user.php 后发现是空的,看了wp才知道,有bak 下载下来直接爆破 但是那个1990是蛮骚的 直接进去登录 登录成功后是空的,走fd看看是怎么过 的 改包然后 ...

  10. 2018-2019-2 20175228实验一《Java开发环境的熟悉》实验报告

    一.实验内容及步骤 (一)使用JDk编译.运行简单的Java程序 实验步骤如下: 实验截图如下: (二)使用IDEA调试程序 1.设置断点2.单步运行:Step Into(快捷捷F7)和Step Ov ...