Write the error prompt page in the Django framework of Python

Source: Internet
Author: User
Tags django website
You should put Template_debugfalse in the production environment if this is set to ' True ', the Django template system will save some extra information for each template in order to display enough on that good-looking error page.
implement a 404 template

If ' DEBUG ' is set to ' True ', Django Displays the 404 error page that comes with it. But if ' DEBUG ' is set to ' False ', then it behaves differently: He will show a template named ' 404.html ' in your template root so when you're ready to deploy your app, you'll need to create the template and put something meaningful in it. " Page not Found "information

Here's an example of a ' 404.html ' that you can start with. Assume that you use a template that inherits and defines a ' base.html ', which consists of titlecontent two blocks.

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

Page not found

Sorry, but the requested page could is not being found.

{% Endblock%}

To test whether your 404.html page is working properly, simply 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 ', the Djang no longer displays its own error feedback page that handles the unhandled Python exception. 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.

Here's a tricky question about 500.html. You can never be sure that ' why ' will show this template, so it should not do anything that needs to connect to the database, or rely on any of the infrastructure artifacts that might be compromised. (for example, it should not use custom template tags.) If it uses template inheritance, then the parent template should not rely on the underlying artifacts that might be compromised. Therefore, the best way is to avoid template inheritance, and to use some very simple things. This is an example of ' 500.html ', which can be used as a starting point:

  Page unavailable  

Page unavailable

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

Our engineers has been notified, so check back later.

Setting error warnings

When you use a Django-made web site to run an exception, you'll want to understand it to make it easier to fix it. By default, Django sends an e-mail to the developer team when your code throws 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 contact that needs attention. This setting takes a tuple similar to (name, Email), like this:

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

Second, make sure that your server is configured to send e-mail. Set up postfix,sendmail or other mail servers that are outside the scope of the book but are related to Django settings, you will need to set Email_host to the correct hostname for your mail server. The default mode is set to ' localhost ', which applies to most shared host system environments. Depending on the complexity of your arrangement, 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 the error e-mail that Django uses. By default it is set to ' [Django] '
Setting up connection Interruption alerts

If you have Commonmiddleware installed (for example, your middleware_classes settings contain ' django.middleware.common.CommonMiddleware ' case, By default, Commonmiddleware is installed, and you have the ability to set this option: if someone accesses a non-empty link to your Django website and causes a 404 error to occur and the connection is interrupted, you will receive an email. If you want to activate this feature, set Send_broken_link_emails to True (default to False), and set your managers to be an email address for someone or someone who will receive a message that reports a connection interruption error. Managers uses and admins the same syntax. For example:

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

Please note that the wrong email can be offensive, for 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.