Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
There's an amazingly handy plugin for Django that helps us spot dangerous queries and other problems. Let's take a look at `django-debug-toolbar`.
Sending emails? This panel gives you a preview of your emails right in the Django debug toolbar.
If you want the documentation for django-debug-toolbar, it's right here.
Remember, you don't need the SHOW_PANEL_CALLBACK
setting when you're using django-debug-toolbar locally or on a live server. I did that only because I'm using Workspaces.
-
0:00
Quite often, when you're building a Django site,
-
0:02
you need a sanity check on various bits and pieces.
-
0:05
Did that template actually get used?
-
0:06
How many queries did I run?
-
0:08
How long did all those queries take?
-
0:10
You could of course track all of these things down in the code and
-
0:12
log them out to a file of the terminal.
-
0:15
Raise your hand if you wanna do that?
-
0:16
Yeah, I don't either.
-
0:18
Thanks to Rob Hudson and many o we don't have to do all of that work.
-
0:22
Their project, Django Debug Toolbar, came out several years ago and
-
0:25
quickly became an indispensable part of many Django developers workflow.
-
0:29
Django Debug Toolbar provides us with a huge amount of valuable information
-
0:33
as we're building and yes, debugging our software.
-
0:36
Let's look at how to install and use it.
-
0:39
Installing Django Debug Toolbar is actually just remarkably simple.
-
0:43
It used to be a bit more involved and if you're working on an older Django project,
-
0:46
you might see more settings related to the toolbar.
-
0:49
In fact, we have to do a special thing now just because we're on workspaces but
-
0:53
I'll walk you through that in a minute.
-
0:55
We're gonna to start by installing the debug toolbar with pip.
-
0:59
So pip install django-debug-toolbar.
-
1:04
And I actually know that for running things in a new workspace, you don't see
-
1:08
this but I fork workspaces a whole lot when I'm creating these courses for you.
-
1:12
I need to install markdown too, so I'm gonna go ahead and do that.
-
1:17
All right, so we have two are installed and
-
1:21
now we need to go to our settings file.
-
1:24
Settings.pi and we're gonna come down here and in installed apps,
-
1:30
after static files, we're going to add debug toolbar.
-
1:37
And you can put it after courses if you want,
-
1:39
but I like to always have my own custom things that I built
-
1:43
listed last just because that way, I'm kinda know where things are.
-
1:48
You have to come in after static files though because Django the debug
-
1:51
toolbar needs some static files.
-
1:53
So here's the thing that's special that we have to add ourselves.
-
1:57
So, let me explain a bit of how this works.
-
1:59
Django debug toolbar expects that all the requests coming to your app,
-
2:04
it checks to make sure that the setting debug is set to true and
-
2:08
it also checks to make sure that you're coming from your localhost.
-
2:12
So from 127.0.0.1.
-
2:15
So, we can do this setting in two different ways.
-
2:20
First thing is we have to add in debug toolbar config.
-
2:25
And this is a dictionary.
-
2:27
So we have two settings that we can set,
-
2:29
one of these is show toolbar callback and the other is internal IPs.
-
2:33
Internal IPs is a little bit easier.
-
2:35
We can just do internal_IPs, and then this is set to a list of IPs.
-
2:42
So the one that we want to set it to is we'd want to have say, 127.01.01.1.
-
2:50
And maybe we don't wanna have ::1 for the IP6.
-
2:53
And then we can just do all zeroes and
-
2:58
everything's considered an internal IP, I do believe.
-
3:02
But, this is a little bit messy and it's a little weird.
-
3:06
So, instead of doing that one,
-
3:07
I'm actually going to do something that's arguably even more messy.
-
3:11
But, it's explicitly like weird so I'll remember to take it out.
-
3:16
So I'm gonna make a new key here that is SHOW_TOOLBAR_CALLBACK.
-
3:21
And so this is a function to be run to determine whether or
-
3:24
not to show the toolbar.
-
3:25
So this gonna be an actual function that you write or
-
3:27
this can be a dotted path to a function somewhere else.
-
3:31
I'm just gonna write a lambda and it's gonna get an argument.
-
3:33
I don't even care what the argument is, I just wanna return true.
-
3:37
All right, so, we added it to installed apps and
-
3:41
then simply because we're on work spaces, we added this.
-
3:44
You will not need these last three lines.
-
3:47
If you're working on your local machine, leave those out.
-
3:50
You don't need these at all.
-
3:51
And you don't want to put these on to a remote server.
-
3:55
All right, so we've saved that and now let's go run our server.
-
4:05
Oops, sorry I got to get into learning site.
-
4:11
And there we go.
-
4:12
And then we can preview our site on port 8000 and
-
4:16
look, here's our debug toolbar, isn't that cool?
-
4:20
So, this is our toolbar and one of the things that you know right away,
-
4:23
is that you can hide it by clicking this hide button.
-
4:26
And now you have this little tab that sticks out,
-
4:28
you can click that to bring out the toolbar again.
-
4:31
Django Debug Toolbar is really smart.
-
4:32
It will remember whether or not you wanted hidden.
-
4:36
And if you said to hide it, then it won't show up on page except there's a little
-
4:39
button and if you leave it open, then it'll be open all the pages.
-
4:42
So, let's look at what's below the hide button.
-
4:45
There's a lot of stuff here.
-
4:46
So this version's tab tells us the version of things that we are running.
-
4:51
So we have 1.4 the toolbar, 1.9 of Django and 3.5.0 of Python.
-
4:56
So that's cool.
-
4:58
The time tab tells us how long the page the pager whee're on took to render and
-
5:02
this is both in Python and in the browser.
-
5:06
So this is kinda cool.
-
5:07
You can kind of see like what's taken the most time,
-
5:09
look we're spending a whole lot of time in the CPU.
-
5:12
Maybe we can get rid of that kind of stuff.
-
5:15
Settings, it just shows you all of your settings.
-
5:18
There's a lot of settings.
-
5:20
Headers shows you the HTTP headers and
-
5:23
the WSGI environ settings for your current page, your current view.
-
5:31
Request, that's our next one and this shows us information about the request.
-
5:34
This is what view we're in, we're in the hello world view.
-
5:37
There were no arguments, there were no keyword arguments, and a URL name is home.
-
5:41
So this is really handy for debugging like how did I get the view that I'm in?
-
5:47
SQL shows us how many queries were run on the page that we're on.
-
5:50
So currently there was one query and it took 19 and a half milliseconds.
-
5:54
So that's cool.
-
5:55
And this cell, that shows you like what was actually run and everything.
-
6:01
And then the expel, E-X-P-L,
-
6:03
will show you the explanation of the query if your engine supports it.
-
6:09
So pretty handy.
-
6:10
We're gonna be using this panel a lot.
-
6:11
So just kinda get used to it.
-
6:14
Static files shows you all the static files that were loaded for
-
6:17
the particular page that you're on.
-
6:20
What's cool though,
-
6:21
is it shows you the apps that have static files that are being loaded.
-
6:25
And then also like, everywhere that's being looked at for static files and
-
6:29
all that stuff.
-
6:30
So this can be really really handy for debug.
-
6:33
Templates works very similarly.
-
6:35
These are the templates that were loaded.
-
6:37
So you can see that we loaded home.html,
-
6:39
we loaded layout.html, and we loaded coursenav.html.
-
6:44
And we also get the context processors that handle
-
6:46
putting things into the template.
-
6:49
If we had a cache, then the cache stuff would show up.
-
6:53
Signals run depending on what's happening in your Django app.
-
6:57
So like when you save a model, there is a pre-save and
-
7:01
post save signals that are fired off.
-
7:05
So the show you which signals are like
-
7:08
involved in the requests that we're currently on.
-
7:10
We'll, do more of these in a workshop or something later on.
-
7:14
And the last panel is this logging panel and if we were sending any messages out to
-
7:18
the logging system that Django go has, they would show up here.
-
7:22
Pretty handy to be able to see like I logged that but I can still look at it.
-
7:25
The last thing on the panel is this thing down here that has intercept redirects,
-
7:31
if you check this box.
-
7:33
And then you go to a view that would do a redirect.
-
7:35
So like you submit a form the form, hits the view the view is like yeah everything
-
7:39
is good and does a redirect to another page.
-
7:42
Checking that box would stop that redirect.
-
7:45
This is really handy, if you want to be able to see in the debug toolbar
-
7:49
what happens after your submit a post.
-
7:52
Most the time I leave this off until I need it because it's one of those thingsll
-
7:55
just get in your way.
-
7:57
And the checkmarks also let you turn off panels.
-
7:59
If you don't gonna be using them like if we don't care about the settings panel.
-
8:03
Then we can turn off the settings panel.
-
8:05
Sometimes that's handy sometimes it's not leave it up for you.
-
8:08
The debug panel will help us a lot with our Django development and fine tuning.
-
8:13
You should go do a search on pipe API for debug panel and
-
8:15
see just how many packages are out there to add new panels to the Django debug tool
-
8:19
bar I'll put a link to the toolbars documentation in the teachers note too
You need to sign up for Treehouse in order to download course files.
Sign up