這篇文章將為大家詳細講解有關(guān)django404定義跳轉(zhuǎn)頁面的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、成都微信小程序、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了沅江免費建站歡迎大家使用!
Django下定義404頁面跳轉(zhuǎn)方法如下
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')],
DEBUG = False ALLOWED_HOSTS = ['localhost','127.0.0.1'] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'sign', #添加sign 應(yīng)用 ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', # 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'autotest.urls' 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,HttpResponseRedirect from django.views.decorators.csrf import csrf_exempt from django.shortcuts import render from django.shortcuts import render_to_response
5.修改urls.py,代碼如下
添加:
handler404=views.page_not_found handler500=views.page_error
如下
from django.conf.urls import url from django.contrib import admin from sign import views #導(dǎo)入sign 應(yīng)用views 文件 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/$', views.index), #添加index/路徑配置 url(r'^create_action/$', views.create_action), url(r'^creating/$', views.creating), url(r'^home/$', views.home), url(r'^buycrouse/$', views.buycrouse), url(r'^tologin/$', views.tologin), url(r'^login_action/$', views.login_action), url(r'^manage/$', views.manage), url(r'^$',views.home), url(r'^accounts/login/$', views.tologin), ] handler404=views.page_not_found handler500=views.page_error
關(guān)于django404定義跳轉(zhuǎn)頁面的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。