Tutorial on customizing the 404,500 page using Django, django404
1. Create a project
django-admin.py startproject HelloWorld
Contains two files.
3. Modify settings. py
(1 .) DEBUG to False, (2 .) ALLOWED_HOSTS Add the specified domain name or IP address, (3 .) specify the template path '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. Create a new view. 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. Modify urls. py. The Code is as follows:
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
Re-compile, restart uwsgi, enter localhost/HelloWorld/test, and display 'Hello World! The entered address will display the content in 404.html. If an error occurs, the content in 500.html will be displayed.