Check out a free preview of the full Practical Guide to Python course

The "Django Admin" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina uses the Django admin interface to manage the content of the blog. The admin can also manage user authentication and permissions.

Preview
Close

Transcript from the "Django Admin" Lesson

[00:00:00]
>> I wanna quickly show you the Django admin and then touch on tests briefly for how you might run them in Django, and then we'll move on to the final project practice. The Django admin is really one of the star features of this web framework and it's one of the reasons why it's so popular.

[00:00:21]
And let's see usually interact with and edit out database models in a really nice UI but very minimal setup. So to use that admin, you're going to need to create a super user. So we do that by running python manage.py create super user, don't forget the password that you set.

[00:00:53]
That will let you know a super user was created successfully. If we go back to URLs.py for practical blog we'll see that there is an admin path to find here. You don't have to use the admin if you don't want to. You would just leave this out. Now that I've created a super user I can go to my blog and instead of looking at a post, I'm going to type in the URL/admin.

[00:01:17]
And that will take me to a login page and I can now log in with the super user that I created and it's gonna take me to this interface. Now, these were created by Django automatically, but here we'll see in the blog app our posts, and I can click in here and add a post and give it a title.

[00:01:52]
And give it a slog and just save it and we'll see that it was added successfully. So when we go back to the index for our blog, there it is, that's the second blog post. And all that I had to do to set that up was create and fill in an admin.py file because it was already created for us.

[00:02:14]
Import our post model and then register it. So admin.site.register post gave us all of that almost for free. If we wanted to have Django auto generate our slugs for us when creating a new blog post, which I think we do because creating any slug manually is pretty annoying.

[00:02:37]
We can comment this out and instead create a class called Post Admin, that inherits from model admin, and then register this instead. If I save that and then refresh this page and go to our admin and we'll notice that Django is currently running in debug mode. That means that it's gonna pick up any changes automatically and just refresh the server.

[00:03:17]
You don't want that on in production. So just an FYI that's a setting that you will flip in settings.py If you go to a URL here that doesn't exist. You're gonna get a page not found error, It's gonna tell you what it was raised by. And then you'll see a message at the bottom saying you're seeing this error because you have debug equals true.

[00:03:38]
In production, you probably want to redirect to a standard 404 page. So now if we look at our admin for post, we will see that when we make a post, As I'm typing in our title. We'll see that this log is generated automatically. The cool thing about this is it skips certain words like a and that you don't really care about for SEO.

[00:04:15]
There we go, so that is what you get with Django for free. There's a lot more that you can do with it, it's very powerful, but I just wanted to give you a preview.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now