Django实现自定义404,500页面教程
作者:YoYong 时间:2021-06-20 06:23:07
1.创建一个项目
django-admin.py startproject HelloWorld
2.进入HelloWorld项目,在manage.py的同一级目录,创建templates目录,并在templates目录下新建404.html,500.html两个文件。
3.修改settings.py
(1.)DEBUG修改为False,(2.)ALLOWED_HOSTS添加指定域名或者IP,(3.)指定模板路径 ‘DIRS' : [os.path.join(BASE_DIR,‘templates')],
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['localhost','www.example.com', '127.0.0.1']
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
4.新建一个views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def hello(request):
return HttpResponse('Hello World!')
@csrf_exempt
def page_not_found(request):
return render_to_response('404.html')
@csrf_exempt
def page_error(request):
return render_to_response('500.html')
5.修改urls.py,代码如下
from django.conf.urls import url
from django.contrib import admin
import HelloWorld.views as view
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test$', view.hello),
]
handler404 = view.page_not_found
handler500 = view.page_error
重新编译,重启uwsgi,输入localhost/HelloWorld/test,显示'Hello World!',输入其它地址会显示404.html内容,如果出错则显示500.html内容。
来源:http://www.yoyong.com/archives/987
标签:Django,自定义404
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中关于CIFAR10数据集的使用
2021-04-14 22:08:05
![](https://img.aspxhome.com/file/2023/7/105757_0s.jpg)
如何基于Python实现word文档重新排版
2023-06-09 11:19:24
![](https://img.aspxhome.com/file/2023/8/128468_0s.png)
创意方法杂谈
2009-05-13 12:53:00
![](https://img.aspxhome.com/file/UploadPic/20095/13/1033_banner-97s.jpg)
如何基于Python实现数字类型转换
2023-10-08 00:57:13
Python如何测试stdout输出
2023-10-22 23:59:56
Python设计模式之工厂方法模式实例详解
2021-02-26 05:40:59
![](https://img.aspxhome.com/file/2023/9/76129_0s.png)
网站数据库,是选SQL Server还是Access好
2008-05-23 13:19:00
Python爬虫之m3u8文件里提取小视频的正确姿势
2021-07-05 18:02:41
![](https://img.aspxhome.com/file/2023/0/103180_0s.png)
flask设置cookie
2022-03-19 21:13:01
如何使用python爬虫爬取要登陆的网站
2022-09-07 08:04:46
![](https://img.aspxhome.com/file/2023/6/105066_0s.jpg)
SQL Server小知识:Processor Affinity
2008-11-24 20:50:00
python异常处理try的实例小结
2022-01-25 06:06:51
Python 实现「食行生鲜」签到领积分功能
2023-02-25 16:26:00
![](https://img.aspxhome.com/file/2023/4/130794_0s.jpg)
Python实现subprocess执行外部命令
2021-10-04 13:42:27
![](https://img.aspxhome.com/file/2023/1/128801_0s.png)
网站图片与文本谁更重要?(中英文对照)
2008-10-17 10:25:00
Python3实现的反转单链表算法示例
2021-09-22 01:33:30
python区块链实现简版工作量证明
2021-03-21 18:08:22
Python实现仿射密码的思路详解
2021-04-17 22:32:00
![](https://img.aspxhome.com/file/2023/4/115684_0s.png)
Python基础篇之字符串方法总结
2022-11-25 11:42:36
初学python数组的处理代码
2023-10-14 19:30:19