Compile the error prompt page in the Python Django framework

Source: Internet
Author: User
Tags django website
This article describes how to write error prompt pages in the Python Django framework, including the traditional 404 page and setting connection interruption warnings, for more information, see TEMPLATE_DEBUGFalse in the production environment. If this parameter is set to 'true', You need to display enough information on the nice-looking error page, the Django template system saves some additional information for each template.
Implement a 404 Template

If ''debug'' is set to ''true', Django will display the built-in 404 error page. However, if ''debug'' is set to ''false'', its behavior will be different: it will display a template named ''404.html ''in the root directory of your template. Therefore, when you are preparing to deploy your application, you will need to create this template and put some meaningful "Page not found" information in it

Here is an example of ''404.html ''from which you can start. Assume that the template you use inherits and defines a ''base.html ''. The page consists of two parts: titlecontent.

{% extends "base.html" %}{% block title %}Page not found{% endblock %}{% block content %}Page not found

Sorry, but the requested page could not be found.

{% endblock %}

To test whether your 404.html page works properly, you only need to set DEBUG to ''false'' and access a URL that does not exist. (It will work on ''sunserver'' as well as on the development server)
Implement a 500 Template

Similarly, if ''debug'' is set to ''false'', Djang will no longer display its own error feedback page for handling unprocessed Python exceptions. Instead, it looks for a template named ''500.html ''and displays it. Like ''404.html '', this template should be placed in the root directory of your template.

There is a tough issue about 500.html. You are never sure why this template is displayed. Therefore, you should not connect to the database or rely on any basic component that may be damaged. (For example, it should not use custom template tags .) If it uses template inheritance, the parent template should not depend on the basic components that may be damaged. Therefore, the best way is to avoid template inheritance and use something very simple. This is an example of ''500.html '', which can be used as a starting point:

  Page unavailable  Page unavailable  

Sorry, but the requested page is unavailable due to a server hiccup.

Our engineers have been notified, so check back later.

Set error warning

If an exception occurs during the running of the website you created using Django, you may want to know about it so as to fix it. By default, Django sends an Email to the developer team when your code causes an unhandled exception. But you need to do two things to set this behavior.

First, change your ADMINS settings to introduce your e-mail address and the e-mail address of any contacts that need attention. This setting uses tuples similar to (name, Email), such:

ADMINS = (  ('John Lennon', 'jlennon@example.com'),  ('Paul McCartney', 'pmacca@example.com'),)

Second, make sure that your server is configured to send emails. If postfix, sendmail, or other email servers outside the scope of this book but related to Django settings are set, you need to set EMAIL_HOST to the correct host name of your email server. the default mode is set to 'localhost'. This setting applies to the system environment of most shared hosts. depending on the complexity of your schedule, you may also need to set EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, EMAIL_PORT, or EMAIL_USE_TLS.

You can also set EMAIL_SUBJECT_PREFIX to control the prefix of error e-mail used by Django. It is set to '[Django]' by default.
Set connection interruption alarm

If you have installed CommonMiddleware (for example, your MIDDLEWARE_CLASSES settings include 'django. middleware. common. in the case of CommonMiddleware, CommonMiddleware is installed by default), you have the ability to set this option: when someone accesses a non-empty link on your Django website and causes a 404 error and connection interruption, you will receive an email. if you want to activate this feature, set SEND_BROKEN_LINK_EMAILS to True (default value: False), and set your MANAGERS to the email address of someone or someone, these email addresses will receive emails that report connection interruption errors. MANAGERS uses the same syntax as ADMINS. for example:

MANAGERS = (  ('George Harrison', 'gharrison@example.com'),  ('Ringo Starr', 'ringo@example.com'),)

Please note that wrong emails will be objectionable to anyone.

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.