Tutorial on customizing the 404,500 page using Django, django404

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.