{"id":127,"date":"2011-09-07T13:47:56","date_gmt":"2011-09-07T13:47:56","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/rprasad\/?p=127"},"modified":"2011-09-07T13:48:56","modified_gmt":"2011-09-07T13:48:56","slug":"django-debugging-sql-queries-in-a-template","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/rprasad\/2011\/09\/07\/django-debugging-sql-queries-in-a-template\/","title":{"rendered":"django: debugging sql queries in a template"},"content":{"rendered":"<p>This link to a 3-step Django snippet created in 2007 still works for showing queries within a template:<\/p>\n<p style=\"padding-left: 30px\"><a href=\"http:\/\/djangosnippets.org\/snippets\/93\/\">http:\/\/djangosnippets.org\/snippets\/93\/<\/a><\/p>\n<p>It&#8217;s helpful for debugging pages.\u00a0 For example, adding the template snippet from the link above will show something like this at the bottom of your pages:<\/p>\n<h2><a href=\"http:\/\/blogs.law.harvard.edu\/rprasad\/files\/2011\/09\/Screen-shot-2011-09-07-at-9.36.17-AM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-128\" src=\"http:\/\/blogs.law.harvard.edu\/rprasad\/files\/2011\/09\/Screen-shot-2011-09-07-at-9.36.17-AM-300x195.png\" alt=\"\" width=\"300\" height=\"195\" srcset=\"https:\/\/archive.blogs.harvard.edu\/rprasad\/files\/2011\/09\/Screen-shot-2011-09-07-at-9.36.17-AM-300x195.png 300w, https:\/\/archive.blogs.harvard.edu\/rprasad\/files\/2011\/09\/Screen-shot-2011-09-07-at-9.36.17-AM-1024x665.png 1024w, https:\/\/archive.blogs.harvard.edu\/rprasad\/files\/2011\/09\/Screen-shot-2011-09-07-at-9.36.17-AM.png 1046w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/h2>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>For step 3 in the link above, &#8220;Use <a href=\"http:\/\/www.djangoproject.com\/documentation\/templates_python\/#subclassing-context-requestcontext\">RequestContext<\/a> when rendering the current template&#8221;, include the following in your view file:<\/p>\n<pre>from django.template import <strong>RequestContext<\/strong>\r\nfrom django.shortcuts import <strong>render_to_response<\/strong>\r\n\r\ndef view_news_page_by_slug(page_slug):\r\n    # build your page, etc\r\n    lu = { 'title': 'hooray', 'story': 'blah, blah, blah' }\r\n    return <strong>render_to_response<\/strong>('your_template.html', lu, context_instance=<strong>RequestContext<\/strong>(request))<\/pre>\n<p>The <a href=\"http:\/\/djangosnippets.org\/snippets\/93\/\">template code from the django snippet<\/a> is fairly straightforward.<\/p>\n<p>To see queries from the python shell, see the django documentation for &#8220;<a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/faq\/models\/#how-can-i-see-the-raw-sql-queries-django-is-running\">How can I see the raw SQL queries Django is running?<\/a>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This link to a 3-step Django snippet created in 2007 still works for showing queries within a template: http:\/\/djangosnippets.org\/snippets\/93\/ It&#8217;s helpful for debugging pages.\u00a0 For example, adding the template snippet from the link above will show something like this at the bottom of your pages: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; For step 3 in &hellip; <a href=\"https:\/\/archive.blogs.harvard.edu\/rprasad\/2011\/09\/07\/django-debugging-sql-queries-in-a-template\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">django: debugging sql queries in a template<\/span><\/a><\/p>\n","protected":false},"author":3875,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4JC3p-23","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/users\/3875"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":4,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/127\/revisions\/132"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}