国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Django實現(xiàn)自定義404,500頁面教程

2020-02-23 04:30:53
字體:
供稿:網(wǎng)友

1.創(chuàng)建一個項目

django-admin.py startproject HelloWorld

2.進入HelloWorld項目,在manage.py的同一級目錄,創(chuàng)建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 = FalseALLOWED_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 HttpResponsefrom django.shortcuts import render_to_responsefrom django.views.decorators.csrf import csrf_exempt@csrf_exemptdef hello(request): return HttpResponse('Hello World!')@csrf_exemptdef page_not_found(request): return render_to_response('404.html')@csrf_exemptdef page_error(request): return render_to_response('500.html')

5.修改urls.py,代碼如下

from django.conf.urls import urlfrom django.contrib import adminimport HelloWorld.views as viewurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^test$', view.hello),]handler404 = view.page_not_foundhandler500 = view.page_error

重新編譯,重啟uwsgi,輸入localhost/HelloWorld/test,顯示'Hello World!',輸入其它地址會顯示404.html內(nèi)容,如果出錯則顯示500.html內(nèi)容。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 治县。| 米泉市| 霍林郭勒市| 平遥县| 佛冈县| 咸丰县| 永新县| 岗巴县| 左贡县| 濮阳县| 东乌珠穆沁旗| 嵊泗县| 类乌齐县| 金坛市| 荣昌县| 偏关县| 牡丹江市| 台安县| 台山市| 洪江市| 临清市| 大竹县| 比如县| 吉首市| 琼中| 西平县| 和田市| 昌乐县| 攀枝花市| 巴林左旗| 仁布县| 巧家县| 乳源| 孟连| 西贡区| 富平县| 双鸭山市| 浦城县| 南宫市| 阿合奇县| 天等县|