Start a free Courses trial
to watch this video
In this video we look at how to set up the Django admin site, which allows you to manage the records in your database through the models defined in your project
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up[?mellow guitar music?] 0:00 [Think Vitamin Membership - Est. 2010] [membership.thinkvitamin.com] 0:03 [Django - Admin Site Setup with Jim Hoskins] 0:07 So far in our project, we've set up a couple of models 0:13 and played around with them from the shell. 0:16 Now we're going to set up the Django admin site so we can manage our models 0:18 from the browser. 0:21 So far, we've defined two different models in our models.py file: 0:23 the Poll model and the Choice model. 0:27 Now, we've had a chance to play around with these on the command line 0:31 and create new polls and query them out of the database, 0:34 but ultimately we actually want to be able to modify and interact with our models 0:37 from a web browser. 0:41 So there are a couple of different ways we could approach this. 0:43 The first thing is we could try to make a full system for creating polls and updating polls, 0:45 and managing them all by ourselves by using the Django framework, 0:50 but ultimately, our application for our end users really only needs to be able to take the polls-- 0:54 that is display a poll--and receive choices from the user. 1:00 The actual administration of creating polls and choices-- 1:04 it really isn't that much of a concern for us. 1:07 We just want a usable application that will allow us to manage those things from the back end. 1:10 Fortunately, Django comes with an administration system 1:15 that's very, very good for this use. 1:18 So in this video, we're going to go ahead and set up our admin application 1:21 so we can manage our polls and choices, among the other models that are in our project. 1:25 So the first thing we need to do is we need to activate the admin site. 1:29 Now, to do this, we're going to go to our mysite settings.py file 1:33 and if we scroll to the bottom, we're going to see this INSTALLED_APPS, 1:39 where we most recently added our Polls app to the application. 1:42 So you'll see some comments down here, and if you don't have comments, 1:46 you can go ahead and just type this in, 1:49 but what we're going to do is we're going to add the django.contrib.admin application 1:51 into our project. 1:56 I'm going to go ahead and remove this line here. 2:00 We could also uncomment this line in order to add the administration docs to it, 2:03 but right now, I'm just going to leave this as is. 2:08 Now, you need to remember that there has to be commas 2:11 between each of our different applications here, so if you don't have a comma 2:14 before django.contrib.admin, make sure to add one in. 2:19 Now, since we're installing a new application, we do have to synchronize our database 2:23 to update if there are any models that django.contrib.admin 2:28 needs to add to the database. 2:32 So to do that, we're going to switch over to our command prompt 2:34 and I am inside of our mysite project directory, 2:36 and what we're going to do is we're going to run the same syncdb command 2:39 that we've run a few times before, so that's python manage.py 2:43 and then syncdb. 2:48 Now, you'll see it created a table for django_admin_log, 2:51 and that's all it needs to do. 2:55 Now finally, there's one last thing we have to do. 2:57 Since the Django admin application is going to be accessible from a web browser, 2:59 we have to modify our urls.py file to tell it exactly where we want it to be accessed from. 3:03 So we're going to flip back to our text editor here inside the mysite directory, 3:10 under urls.py, under this line where it says "Uncomment the next line to enable the admin" 3:15 we're going to use this particular pattern. 3:21 And so this is the first time we've actually looked at the urls.py 3:25 and this is sort of the main table of contents for our application for mapping the urls 3:30 to the various views on our site. 3:35 Now, at it's most basic level, 3:38 what it's doing here is it's matching anything that starts with admin/ 3:39 and it's actually delegating it to a different url file--in this case, (admin.site.urls), 3:44 and that means that this particular module here is what's actually going to be handling 3:50 any of the url handling below the /admin line. 3:55 And so, since the admin application is a full application in itself, 4:00 we really don't have to worry about it--we just need to include this one line 4:04 saying to include it in our urls file. 4:07 Now, before we can actually use this, there's a couple of lines at the top that we also need 4:10 to uncomment, so we're going to uncomment line 4 here and line 5. 4:14 And what this does is it means we're including the django.contrib admin module, 4:19 and we're using this admin.autodiscover function. 4:24 Now what this does is it will find all of our models 4:27 and it will make them available from the admin. 4:30 And now that we've included it, we can now use the admin.sites.urls. 4:33 So once we save this out, we want to go to our terminal 4:38 and start up our server. 4:41 So we're going to use the Python manage.py runserver command 4:43 and once we got that we're going to navigate to the url that it gives us, 4:48 and if we go to the root url, we'll see that we get a Page not found, 4:52 and that's because we've now defined some urls, but not one for the root path. 4:55 So let's go to /admin and now we'll see this Django administration login. 4:59 Now, we're just going to use the Super User account that we created a few videos back 5:04 when we set up our database. 5:10 Now, if you don't remember the username and the password that you can set up, 5:12 we can easily set up a new one from the command line. 5:16 So what we're going to do is flip over to our terminal 5:19 and from the mysite directory, we're going to run python manage.py, 5:22 we'll use the createsuperuser command 5:28 and then we're going to pass it - - username= and then the username you want to use. 5:35 In this case, I'll say jim, and we'll give it an email=jim@thinkvitamin.com. 5:41 And now it's going to ask you for a password for that user, 5:51 so we can just set up a password, 5:55 and now we've created another Super User. 6:00 Now, again, if you remember the Super User credentials that you set up originally 6:03 when we ran syncdb, you can use that. 6:06 This is just if you forget your password and need to create a new one. 6:09 So I'm going to log in with my original one, 6:12 and I'll just type in the username and password here, 6:15 and now we're logged into our Django administration. 6:19 We'll make this a little bit bigger. 6:23 And so here, we can see a couple of different tables. 6:25 This first table here says Auth and this is for our authentication applications, 6:28 so we can manage the Groups and the Users from this panel. 6:32 Second, we'll see the Sites application, which allows us to create sites 6:36 in our Django project. 6:40 We'll look a little more into that later, but we're missing a very important application-- 6:42 our Polls application, which will have our Polls and our Choices. 6:46 So up next, we're going to take a look at how to get that set up. 6:50 So now we have the Django admin site working in our project. 6:54 In the next video, 6:57 we're going to configure the admin site to work with our custom Polls application. 6:58 [?mellow guitar music?] 7:02 [Think Vitamin Membership - Est. 2010] [membership.thinkvitamin.com] 7:04
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up