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