How to process error pages in Django: how to process error pages in django

Source: Internet
Author: User
Tags django server timedelta

How to process error pages in Django: how to process error pages in django

Let's take a few minutes to appreciate the Web application we have written, and then let's make some small damage. We intentionally introduced a Python error in the views. py file and commented out a row of offset = int (offset) in the hours_ahead view.

def hours_ahead(request, offset):  # try:  #   offset = int(offset)  # except ValueError:  #   raise Http404()  dt = datetime.datetime.now() + datetime.timedelta(hours=offset)  html = "

Start the development server and access/time/plus/3 /. You will see an error page containing a large amount of information. The top TypeError message is: "unsupported type for timedelta hours component: unicode ".

What's going on? Yes, the datetime. timedelta function requires the hours parameter to be an integer, And we have commented out the code to convert offset to an integer. This causes the TypeError exception to pop up in datetime. timedelta.

This example shows the error page of Django. Let's take a moment to look at this error page and find out what information it provides.

The following are some important points worth noting:

  • At the top of the page, you can get the key exception information: abnormal data type, abnormal parameters (such as "unsupported type" in this example ") the file in which an exception is thrown, and the wrong row number.
  • Below the critical exception information, this page displays the complete Python tracing information for this exception. This is similar to the tracing information you get in the Python command line interpreter, but the latter is more interactive. For each frame in the stack, Django displays the file name, function or method name, row number, and source code of the row.
  • Click this line of code (displayed in dark gray). You can see the lines before and after the error line to learn the context.
  • Click "Local vars" for any frame in the stack to view a list of all Local variables and their values when the frame fails. This debugging information is quite useful.
  • Note the "Switch to copy-and-paste view" text under "Traceback. Click these words to switch back to another view, which makes it easy for you to copy and paste the content. You can use this exception tracing feature when you want to share it with others for technical support (such as in Django's IRC chat room or email list.
  • You can press Share this traceback on a public Web site under the notebook to complete the job. Click it to return the tracing information to http://www.dpaste.com/, where you can get a single URL and share it with others.
  • The subsequent "Request information" section contains a large amount of information about the Web requests that generate errors: GET and POST, cookie value, and metadata (such as CGI header ). The complete reference of the request object is provided in Appendix H.
  • Under the Request information, "Settings" lists the specific configuration information used by Django. (We have mentioned ROOT_URLCONF. Next we will show you various Django settings. Appendix D covers all available settings .)

Django's error page can display more information in some cases, such as template syntax errors. Let's talk about the Django template system. Now, cancel the annotation of the line offset = int (offset) to make it work again.

I wonder if you are a programmer who uses the print statement carefully placed to help debugging? You can use the Django error page instead of the print statement. Insert an assert False temporarily at any position in your view to trigger the error page. Then, you can see local variables and program statements. Here is an example of using the hours_ahead View:

def hours_ahead(request, offset):  try:    offset = int(offset)  except ValueError:    raise Http404()  dt = datetime.datetime.now() + datetime.timedelta(hours=offset)  assert False  html = "

Finally, it is obvious that many of the information is sensitive. It exposes the internal structure of your Python code and the Django configuration. It is silly to disclose this information on the Internet. Malicious people will try to use it to attack your Web application and do something dirty. Therefore, Django error messages are only displayed in debug mode. We will explain how to Disable debug later. Now, you only need to know that the Django server runs in debug mode by default when you enable it. (Sounds familiar? No error is found on the page. As mentioned above, it works normally .)

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.