{"id":75188,"date":"2020-01-18T12:24:53","date_gmt":"2020-01-18T06:54:53","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75188"},"modified":"2024-04-12T14:17:11","modified_gmt":"2024-04-12T08:47:11","slug":"djangos-pretty-error-pages","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/","title":{"rendered":"Django\u2019s Pretty Error Pages"},"content":{"rendered":"<p>Take a moment to admire the fine web application we\u2019ve made so far \u2013 now let\u2019s break it! Let\u2019s deliberately introduce a Python error into our views.py file by commenting out the offset = int(offset) lines in the hours_ahead view:<\/p>\n<p>def hours_ahead(request, offset):<\/p>\n<p># try:<\/p>\n<p>#&nbsp;&nbsp;&nbsp;&nbsp; offset = int(offset)<\/p>\n<p># except ValueError:<\/p>\n<p>#&nbsp;&nbsp;&nbsp;&nbsp; raise Http404()<\/p>\n<p>dt = datetime.datetime.now() + datetime.timedelta(hours=offset)<\/p>\n<p>html = &#8220;&lt;html&gt;&lt;body&gt;In %s hour(s), it will be&nbsp; %s.&lt;\/body&gt;&lt;\/html&gt;&#8221; %<\/p>\n<p>(offset, dt)<\/p>\n<p>return HttpResponse(html)<\/p>\n<p>Load up the development server and navigate to \/time\/plus\/3\/. You\u2019ll see an error page with a significant amount of information, including a TypeError message displayed at the very top: \u201cunsupported type for timedelta hours component: str\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-128526 size-full\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/4.jpg\" alt=\"\" width=\"1039\" height=\"787\"><\/p>\n<p>What happened? Well, the datetime.timedelta function expects the hours parameter to be an integer, and we commented out the bit of code that converted offset to an integer. That caused datetime.timedelta to raise the TypeError. It\u2019s the typical kind of small bug that every programmer runs into at some point.<\/p>\n<p>The point of this example was to demonstrate Django\u2019s error pages. Take some time to explore the error page and get to know the various bits of information it gives you. Here are some things to notice:<\/p>\n<ul>\n<li>At the top of the page, you get the key information about the exception: the type of exception, any parameters to the exception (the &#8220;unsupported type&#8221; message in this case), the file in which the exception was raised, and the offending line number.<\/li>\n<li>Under the key exception information, the page displays the full Python traceback for this exception. This is similar to the standard traceback you get in Python\u2019s command-line interpreter, except it\u2019s more interactive. For each level (\u201cframe\u201d) in the stack, Django displays the name of the file, the function\/method name, the line number, and the source code of that line.<\/li>\n<li>Click the line of source code (in dark gray), and you\u2019ll see several lines from before and after the erroneous line, to give you context. Click \u201cLocal vars\u201d under any frame in the stack to view a table of all local variables and their values, in that frame, at the exact point in the code at which the exception was raised. This debugging information can be a great help.<\/li>\n<li>Note the \u201cSwitch to copy-and-paste view\u201d text under the \u201cTraceback\u201d header. Click those words, and the traceback will switch to an alternate version that can be easily copied and pasted. Use this when you want to share your exception traceback with others to get technical support \u2013 such as the kind folks in the Django IRC chat room or on the Django users mailing list.<\/li>\n<li>Underneath, the \u201cShare this traceback on a public web site\u201d button will do this work for you in just one click. Click it to post the traceback to dpaste, where you\u2019ll get a distinct URL that you can share with other people.<\/li>\n<li>Next, the \u201cRequest information\u201d section includes a wealth of information about the incoming web request that spawned the error: GET and POST information, cookie values, and meta information, such as CGI headers. Appendix F has a complete reference of all the information a request object contains.<\/li>\n<li>Below the \u201cRequest information\u201d section, the \u201cSettings\u201d section lists all of the settings for this particular Django installation.<\/li>\n<\/ul>\n<p>The Django error page is capable of displaying more information in certain special cases, such as the case of template syntax errors. We\u2019ll get to those later, when we discuss the Django template system. For now, uncomment the offset = int(offset) lines to get the view function working properly again.<\/p>\n<p>The Django error page is also really useful if you are the type of programmer who likes to debug with the help of carefully placed print statements.<\/p>\n<p>At any point in your view, temporarily insert an assert False to trigger the error page. Then, you can view the local variables and state of the program. Here\u2019s an example, using the hours_ahead view:<\/p>\n<pre>  def hours_ahead(request, offset):&nbsp;&nbsp;&nbsp;\n\n      try:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n\n          offset = int(offset)&nbsp;&nbsp;&nbsp; \n\n       except ValueError:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \n\n           raise Http404()&nbsp;&nbsp;&nbsp;\n\n       dt = datetime.datetime.now() + datetime.timedelta(hours=offset)&nbsp;&nbsp;&nbsp; \n\n      assert False&nbsp;&nbsp;&nbsp; \n\n      html = \"&lt;html&gt;&lt;body&gt;In %s hour(s), it will be&nbsp; %s.&lt;\/body&gt;&lt;\/html&gt;\" % \n\n(offset, dt)&nbsp;&nbsp;&nbsp; \n\n  return HttpResponse(html)<\/pre>\n<p>Finally, it\u2019s obvious that much of this information is sensitive \u2013 it exposes the innards of your Python code and Django configuration \u2013 and it would be foolish to show this information on the public Internet. A malicious person could use it to attempt to reverse-engineer your web application and do nasty things.<\/p>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Take a moment to admire the fine web application we\u2019ve made so far \u2013 now let\u2019s break it! Let\u2019s deliberately introduce a Python error into our views.py file by commenting out the offset = int(offset) lines in the hours_ahead view: def hours_ahead(request, offset): # try: #&nbsp;&nbsp;&nbsp;&nbsp; offset = int(offset) # except ValueError: #&nbsp;&nbsp;&nbsp;&nbsp; raise Http404()&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8663],"class_list":["post-75188","page","type-page","status-publish","hentry","category-django-web-development","tag-djangos-pretty-error-pages"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Django\u2019s Pretty Error Pages - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Django\u2019s Pretty Error Pages - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Take a moment to admire the fine web application we\u2019ve made so far \u2013 now let\u2019s break it! Let\u2019s deliberately introduce a Python error into our views.py file by commenting out the offset = int(offset) lines in the hours_ahead view: def hours_ahead(request, offset): # try: #&nbsp;&nbsp;&nbsp;&nbsp; offset = int(offset) # except ValueError: #&nbsp;&nbsp;&nbsp;&nbsp; raise Http404()...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/4.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/\",\"name\":\"Django\u2019s Pretty Error Pages - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/4.jpg\",\"datePublished\":\"2020-01-18T06:54:53+00:00\",\"dateModified\":\"2024-04-12T08:47:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#primaryimage\",\"url\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/4.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/4.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Django\u2019s Pretty Error Pages\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Django\u2019s Pretty Error Pages - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/","og_locale":"en_US","og_type":"article","og_title":"Django\u2019s Pretty Error Pages - Tutorial","og_description":"Take a moment to admire the fine web application we\u2019ve made so far \u2013 now let\u2019s break it! Let\u2019s deliberately introduce a Python error into our views.py file by commenting out the offset = int(offset) lines in the hours_ahead view: def hours_ahead(request, offset): # try: #&nbsp;&nbsp;&nbsp;&nbsp; offset = int(offset) # except ValueError: #&nbsp;&nbsp;&nbsp;&nbsp; raise Http404()...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:11+00:00","og_image":[{"url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/4.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/","name":"Django\u2019s Pretty Error Pages - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/4.jpg","datePublished":"2020-01-18T06:54:53+00:00","dateModified":"2024-04-12T08:47:11+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#primaryimage","url":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/4.jpg","contentUrl":"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/4.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/djangos-pretty-error-pages\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Django\u2019s Pretty Error Pages"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75188"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75188\/revisions"}],"predecessor-version":[{"id":83311,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75188\/revisions\/83311"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}