django系列7:修改404页面展示,优化模板,降低urlconf和模板之间的耦合,命名app将模板和app绑定
为了增加程序的友好和健壮性,修改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绑定的更多相关文章
- Django学习路36_函数参数 反向解析 修改404 页面
在 templates 中创建对应文件名的 html 文件 (.html) 注: 开发者服务器发生变更是因为 python 代码发生变化 如果 html 文件发生变化,服务器不会进行重启 需要自己手动 ...
- 学习ASP.NET Core Razor 编程系列七——修改列表页面
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 针对ecshop错误404页面的优化
在ecshop系统当中,比如你随意将商品详细页面的地址中的ID修改为一个不存在的商品ID,ecshop会自动跳转到首页.ecshop在这方面做得非常的差,甚至导致了很多的站不被搜索引擎收录.最模板提供 ...
- 夺命雷公狗---DEDECMS----23dedecms修改内容页面展示的信息
我们在网站上不管点击那个影视作品的A连接都是进入到一个同样的页面,因为他们是一个模版文件: 我们还没有对这个模版进行任何的修改,所以我们要在内容模版增加标签取出对应的影视作品,而且导航条也是按照模版上 ...
- 学习ASP.NET Core Razor 编程系列十二——在页面中增加校验
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- django系列8:优化vote页面,使用通用视图降低代码冗余
修改detail.html,将它变为一个可用的投票页面 <h1>{{ question.question_text }}</h1> {% if error_message %} ...
- 潭州课堂25班:Ph201805201 django框架 第十三课 自定义404页面,auth系统中的User模型,auth系统权限管理 (课堂笔记)
当 DEBUG=True 时,django 内部的404报错信息, 自带的报错信息, 要自定义404信息,要先把 DEBUG=False , 之后要自定义4040页面,有两种方法, 方法1,在创建40 ...
- Django如何自定义漂亮的404页面
目录 在templates 中添加404.html 修改settings.py 在templates 中添加404.html <!DOCTYPE html PUBLIC "-//W3C ...
- 通过修改 Apache 的配置文件 htaccess 文件实现自定义404页面
最近在学习使用Apache服务器的配置,做一个记录. Apache下有个.htaccess文件,是Apache的一个特殊的配置文件.这个配置文件默认是没有的,要手动在各自的项目的根目录编写才行. 要实 ...
随机推荐
- Left Jion和where区别
首先,新建两张表A和B,然后插入6条数据到A表,3条数据到B表.语句如下: create table A( ID ,) not null, AName ) null ) create table B( ...
- 我的第一个python web开发框架(23)——代码版本控制管理与接口文档
书接上一回,小白和老菜聊到代码的版本控制和接口文档 小白:为什么要做版本控制,我不弄版本控制不也完成了项目了吗?要做版本控制不是很麻烦,又要安装服务又要提交代码,代码又不是多人用开发,还要写文档... ...
- 本机不安装Oracle客户端,使用PL/SQL Developer和 Instant Client 工具包连接oracle 11g远程数据库
一.先到Oracle网站下载Instant Client 下载地址:http://www.oracle.com/technetwork/cn/database/features/instant-cli ...
- 进程间数据传递:Queue,Pipe 进程间数据共享:Manager
1.使用multiprocessing模块的Queue实现数据传递 ''' 进程间通讯: Queue,用法跟线程里的Queue一样,put,get 线程queue没有做序列化,进程queue做序列化了 ...
- Error response from daemon: conflict: unable to remove repository reference 解决方案
由于前一章演示用的镜像没什么用准备删除 docker image rm hello-world:latest Error response from daemon: conflict: unable ...
- vue.js 跳转同一页面,传不同值,组件监听路由
watch: { '$route' () { this.type = this.$route.params.type this.loadData() } },
- python3 time模块
import time '''查看系统时间拿到的是一个数字(时间戳)从1970-01-01 00:00:00开始计算,以秒为单位'''print(time.time()) 执行结果:155650817 ...
- Redis常用数据结构
Redis常用数据结构包括字符串(strings),列表(lists),哈希(hashes),集合(sets),有序集合(sorted sets). redis的key最大不能超过512M,可通过re ...
- Centos7.x做开机启动脚本
cat /etc/centos-release CentOS Linux release 7.4.1708 (Core) uname -r 3.10.0-693.11.1.el7.x86_64 vim ...
- 洛谷 P1908 逆序对
\[传送门qwq\] 题目描述 猫猫\(TOM\)和小老鼠\(JERRY\)最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计. 最近,\(TOM\)老猫查阅 ...