1024programmer Java Writing error prompt pages in Python’s Django framework

Writing error prompt pages in Python’s Django framework

You should set TEMPLATE_DEBUGFalse to True in a production environment. Django’s template system will save some additional information for each template in order to display enough on that nice error page.
Implement a 404 template

If “DEBUG“ is set to “True“, Django will display the built-in 404 error page. But if “DEBUG“ is set to “False“, then its behavior is different: it will display a template named “404.html“ in your template root directory. So, when When you’re ready to deploy your application, you’ll want to create this template and put some meaningful “page not found” information in it

Here is an example 404.html that you can start with. Assume that the template you use inherits and defines a “ base.html “, and the page consists of two parts: title and content.

 {% 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 that your 404.html page is working properly, simply set DEBUG to False and access a non-existent URL. (It will work just as well on sunserver as on development server)
Implement a 500 template

Similarly, if “DEBUG“ is set to “False“, Djang will no longer display its own error feedback page for unhandled Python exceptions. Instead, it looks for a template named “500.html“ and displays it. Like “404.html“, this template should be placed in your template root directory.

Here is a tricky question about 500.html. You can never be sure why this template is displayed, so it shouldn’t do anything that requires connecting to a database, or relies on any infrastructure that may be broken. (For example: it shouldn’t use custom template tags.) If it uses template inheritance, the parent template shouldn’t rely on underlying constructs that might be broken. Therefore, the best approach is to avoid template inheritance and use something very simple. Here is an example “500.html“ that can be used as a starting point:




  


   

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

When an exception occurs during the operation of the website you made using Django, you will want to understand it so that you can correct it. By default, Django will send an email to the development team when your code throws an unhandled exception. But you need to do two things to set up this behavior.

First, change your ADMINS settings to include your email address, as well as the email addresses of any contacts that need attention. This setup uses a (Name, Email) tuple, like this:

 ADMINS = (
   ('John Lennon', '[email protected]'),
   ('Paul McCartney', '[email protected]'),
 )

 

Second, make sure your server is configured to send email. To set up postfix, sendmail, or any other mail server that is outside the scope of this book but relevant to your Django setup, you will need to set EMAIL_HOST to the correct hostname of your mail server. The default mode is set to ‘localhost’, which is the setting Applies to most shared hosting 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 used by Django for error e-mails. By default it is set to ‘[Django] ‘
Set a disconnection alert

If you have CommonMiddleware installed (for example, if your MIDDLEWARE_CLASSES setting contains ‘django.middleware.common.CommonMiddleware’, CommonMiddleware is installed by default), you have the ability to set this option: someone is visiting If a non-empty link to your Django site 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 is False), and set Your MANAGERS are the email addresses of a person or groups of people. These email addresses will receive emails reporting connection interruption errors. MANAGERS uses the same syntax as ADMINS. For example:

 MANAGERS = (
   ('George Harrison', '[email protected]'),
   ('Ringo Starr', '[email protected]'),
 )

 

Please note that bad emails can be off-putting to anyone.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/787619

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索