python中常见的报错

常见的错误

2 开头的 一般是成功的

3 开头的 重定向

301 永久性重定向
302 暂时性重定向

4 开头的 一般是客户端的错误

404 找不到目标url
403 你没有权限访问相关的数据
405 请求方法不允许。限制请求的过程中,只允许get请求 但是你非得 post传过去 这个时候报405错误
400 请求的参数有错误

5 开头的 一般是服务器的错误

500 服务器内部错误 代码有 bug
502 一般是 服务器部署错误 比如 nginx启动 但是 uwsgi 有无 没法完成正常的请求

	生产环境  也就是线上。上线以后 会把debug 关闭
	settings.py :
	DEBUG = False
	ALLOWED_HOSTS = ['127.0.0.1']

常见的错误 比如 404 500 直接在 templates 下面 新建 404.html 500.html如果出现 404 500 错误 会自动的显示这个页面的内容

其它错误 比如 400 403

专门 定义一个 app 名字叫 errors
在errors 下面 新建一个 templates 下面再建一个 errors 里边创建 页面:400.html或者 403.html,502.html

在errors 应用下面

views.py

from django.shortcuts import render
def view_400(request):
   	return render(request,'errors/400.html')
def view_403(request):
    return render(request,'errors/403.html')
def view_502(request):
    return render(request,'errors/502.html')

urls.py

from django.urls import path
from . import  views
app_name = 'errors'
urlpatterns = [
    path('400.html',views.view_400,name='400'),
    path('403.html',views.view_403,name='403'),
    path('502.html',views.view_502,name='502'),
]

在 项目总的urls.py下面 :

path('errors/',include('errors.urls'))

猜你喜欢

转载自blog.csdn.net/qq_37816095/article/details/84745212
今日推荐