Django for Designers: Difference between revisions

imported>Paulproteus
imported>Paulproteus
Line 2,120:
==== Installing django-debug-toolbar and what it's useful for ====
 
Django's operation can be somewhat opaque. When a page does not show the information you were expecting, there can a great number of reasons: perhaps some data was not saved to the database, or perhaps the variable name the template was expecting did not match the name you provided in the context from the view.
(explain what it is useful for)
 
Django debug toolbar is an open source Django app that gives you more insight into how Django is working. In this section, we will show you how to install it, demonstrate at least one thing it is useful for, and explain at least one gotcha that you should be aware of.
(install it with pip)
 
Now is a good time to make sure you are using a Django app that actually works! If your current branch is not something you're confident of, this is a good moment to create a new branch based on known-working code. To do that:
(enable it in settings.py)
 
<source lang="bash">
(show how it lets you see the template context when you visit your bookmarks site)
# in django-for-designers
$ git branch my-debug-toolbar-work origin/pre-part-5
$ git checkout my-debug-toolbar-work
</source>
 
First, we will need to install it. Typically, you would need to add it to ''requirements.txt'' for your own project. In the case of the tutorial, we already configured requirements.txt to have it, but we will show you how to add it as if we hadn't.
(mention that it "absorbs" redirects)
 
Open ''requirements.txt'' in your favorite text editor. Make sure the following line appears:
(mention DEBUG=True is required for it to show, which is mostly a good thing)
 
<source lang="python">
django-debug-toolbar
</source>
 
Once that is done, you would run the following command. (It is safe to run it now, even though it may not be needed).
 
<source lang="bash">
# in django-for-designers
pip install -r requirements.txt
</source>
 
This reads requirements.txt and ensures your virtualenv has all the packages installed, downloading and installing them if necessary.
 
Now that it is available, we need to tell Django to enable it. django-debug-toolbar requires a few adjustments to your settings.py, which you can find in ''myproject/settings.py''. First, look for '''MIDDLEWARE_CLASSES'''. Add the following string as the final indented line in the sequence, and make sure there is a comma at the end of the line before it:
 
<source lang="python">
'debug_toolbar.middleware.DebugToolbarMiddleware',
</source>
 
Configure the list of IP addresses will be able to see the debug toolbar. To do that, add this to the end of ''myproject/settings.py'':
 
<source lang="python">
INTERNAL_IPS=('127.0.0.1',)
</source>
 
Finally, make sure it appears in the list of INSTALLED_APPS. If not, added it to the end of that list as follows:
 
<source lang="python'>
'debug_toolbar',
</source>
 
Now that it is installed, stop and start your runserver. Then take a look at http://127.0.0.1:8000/ -- do you see the new toolbar in the top right corner?
 
If so, great!
 
We'll show you two neat tricks the debug toolbar can offer you. First, if you click on the ''SQL'' box, you will see the page expand into a list of all the SQL queries that your front page executed, and how long they took. If your pages are loading slowly, you may find that you can trim down the number or the complexity of these queries.
 
Second, if you click '''Templates''', you can see which templates were rendered, and (excitingly) what data was passed to them. This is the simplest way to see what information was passed to the templates. Although the interface is somewhat complicated, it is more helpful than repeatedly reloading a page with different "print" statements in it!
 
There are two gotchas that one must be aware of when using django-debug-toolbar:
 
# By default, it only works when your DEBUG is True. Most of the time, this is a good fit; it means users out on the 'net can't view this advanced interface. (That's because when you "deploy" an app, you are supposed to set DEBUG to False.)
# It "absorbs" redirects. Any time that your code would generate a redirect in the browser, the debug toolbar "catches" that and lets you see what is going on. This can be great, but it can also get annoying.
 
You can read more about these features, how to change them, and what else the debug toolbar can do on [https://github.com/django-debug-toolbar/django-debug-toolbar its official website]!
 
==== Writing your own tests ====
Anonymous user