django404怎么定义跳转页面

python

Django下定义404页面跳转方法如下

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')],

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 应用

]

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 #导入sign 应用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

以上是 django404怎么定义跳转页面 的全部内容, 来源链接: utcz.com/z/521844.html

回到顶部