Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Django's Admin5:39 with Kenneth Love
One of Django's jaw dropping features is its built-in, auto-generated admin panel.
python manage.py createsuperuser will create a new superuser, or a user that's allowed to log into the admin area with all permissions.
admin.site.register(Model) will register a model with the default admin site, which allows you to edit instances of that model in the admin.
One of Django's killer features, one of the things that you show to old school PHP 0:00 and Perl developers to make their jaws drop is Django's admin. 0:03 It's not that it's groundbreaking or 0:07 that it does something never seen before, it's just the fact that you have to do so 0:09 little work to get something that is so useful. 0:12 Let's go check it out. 0:15 Okay, so I still have my server running and 0:17 by looking at the project URLs, I know that the URL is /admin. 0:20 So let's, so let's see what I get if I just go there. 0:24 Oh what is this? 0:31 I have to log in? 0:32 Okay, that's probably a good idea honestly. 0:33 The last thing I'd want is some random person off the internet logging in and 0:35 messing up my site. 0:38 Okay, so how do I get an account though? 0:39 Do I have to create like a whole authentication system or something? 0:41 Nope. 0:45 Let's just go back to the terminal. 0:46 So let's bump up our terminal here, and 0:48 let's kill our server with CTRL+C, and you're probably sensing a pattern here. 0:51 We have to create something, and there's a Django command for it. 0:56 We're gonna use this one called create super user. 1:01 So we'll do python manage.py createsuperuser. 1:04 All right, so now we give a username, all this kinds of stuff. 1:10 So, we're going to go through the prompts. 1:12 I'm going to name it Kennethlove, and I'll give my treehouse password. 1:13 Sorry, my treehouse email address. 1:23 And, I'll give password will be password. 1:25 And I get that the superuser was created successfully. 1:29 So, great. 1:32 Now you don't have to worry too much about setting an amazing great password 1:33 here since it's just gonna be on your local machine or workspaces. 1:37 But, when you do this for your actual live on the internet site, 1:40 you want to be sure and have a really good password. 1:43 Okay, so let's run the server again. 1:46 And let's go over here and see if we can log in with the stuff that I just made. 1:50 And I did, cool. 1:57 So, I don't want Chrome to remember that. 1:59 When we first log in, we only have two things in the admin. 2:01 Right, we have users and we have groups. 2:04 Now these are two models that are provided by Django, and 2:07 we can see that they're under this authentication and authorization header. 2:10 They belong to Django's off app. 2:14 But Django gave it a custom name in the admin. 2:17 That's why it says authentication and authorization instead of just off. 2:19 Let's see what's in the area here for users. 2:23 Oh look, there's my user account that I created. 2:28 Awesome. Now I can search for 2:31 users, I can filter them by some different statuses over on the side. 2:32 What if I click on my username though? 2:36 Here it is. This is a pretty awesome form for 2:40 changing all of my users details. 2:42 Now you might not believe me just yet, but 2:45 Django's developers had to do very little work to make this exact form show up. 2:46 Let's see what we have to do to get something not exactly like this, 2:52 but useful in the admin. 2:56 Okay, let's go back over here to our courses app, and 2:59 we have this admin.py file. 3:04 If you're guessing that that has something to do with the admin, you're right. 3:08 Okay, so now, we're gonna open that up, and 3:12 we can see that it has a message telling us to register our models. 3:14 All right, well so, first thing we have to do though, is import our model. 3:17 So, from .models import Course. 3:23 And then we're gonna come down here, and we're gonna do admin.site.register. 3:26 So, admin is the admin. 3:32 The site that currently exists, and then we're gonna register something with it. 3:36 What we're gonna register is course. 3:40 So, we've registered our model. 3:43 We see that the server restarted. 3:46 Let's go check out the admin again. 3:48 Let's click on home. 3:50 Look at that! 3:51 Courses is already there. 3:54 All right, let's check that out. 3:55 There's each of the courses that we created in the shell, all right? 3:57 Now, let's look at one of them. 4:00 Let's look at say, Python Basics. 4:01 And there's the form with title and the description field. 4:05 Now, where is the created app field, 4:07 where's the IED that we saw on the migration? 4:08 Well, the ID is automatically set. 4:11 It's auto-incrimenting. 4:14 So, Django's like, okay, you don't need that, hide that away. 4:15 And, kinda the same idea with our created app field. 4:18 We set it to be auto now add. 4:21 We set that as true. 4:23 And so Django goes, okay, 4:25 well you're not going to be editing that because you're setting it automatically. 4:26 So I'll just hide it for you cuz you don't need that. 4:31 Cool. 4:33 Awesome. 4:33 Let's see about creating a new course, all right. 4:35 So let's go home and we can look at courses. 4:36 Now we can actually from here we can just click add, but 4:40 I wanna show you a couple of other ways. 4:42 There's also add course here, so either one of those will work to add a course. 4:45 Or we can go to courses and we can click add there. 4:50 They all go to the same place. 4:53 So we're just gonna click add, and we're gonna add a new course. 4:54 Let's add Python Testing. 4:57 Learn to test your Python applications with unit test and doc tests. 5:00 And we're gonna save. 5:10 And check that out, now we have four courses. 5:11 That is so much easier than creating stuff in the shell. 5:16 Adding model instances through the shell is simple enough. 5:20 But Django's admin is even easier. 5:23 We'll do a bit more with the admin later, but 5:25 fully exploring the admin's power is way beyond the scope of this course. 5:27 Let me know if you'd like a course or workshop on it though. 5:31 All right, that's enough modeling and plaintext views. 5:33 We should learn to use templates so we can have a more attractive and useful site. 5:36
You need to sign up for Treehouse in order to download course files.Sign up