The Django Administration Site

Опубликовано: 05 Апрель 2026
на канале: Code master
5,388
24

1. Lets first take a look at our admin site to see what the default version of admin site looks like before we actually make any modifications. Fire up your server using the following command. 'python3 manage.py runserver'

2. Now visit http://127.0.0.1:8000/admin and we will use our superuser account to login which we created in the previous tutorial.

3. You can see Django has already built us a pretty cool little admin site. On the home page you the menu bar which includes Django title, welcome message and some links. Below that you will find the site administration. Here you will be able to create users, update users and delete users. Also, you will be able to group users. This feature is included with the Django. The authentication includes many of features like handing users, groups, permissions, and cookie-based user sessions. I am not going to dive too deep into this. We will see this feature more throughout the tutorial series. Let's add our post model to the administration site so we can work with it in the admin site.

4. Go ahead and open your admin.py file located in your blog directory. This file only contains one line of code currently. You will see 'from django.contrib import admin' This line allows to work with admin site.

5. Let's add some code so we can add our model to the admin site. First, we need to import our Post model. To do this we need to add the following line below the admin import. 'from .models import Post' now the admin file can access our model. We could have also write this as 'blog.models import Post'. But blog.models is not need since we are in the same directory or module if you will so we just need to .models.

6. Now let's register our model to register we just need to add one line of code which is 'admin.site.register(Post)' now our model is registered.

7. Refresh your admin site you will see now Blog app and post model is now add to the admin site. Pretty cool and that is extremely easy.

8. Let's create a post. Click on the post and then click on 'add post'. Just fill in the post nothing elaborate because we will probably end up deleting and creating as we test out the functions of our site.

9. Now open your models.py go to the bottom of our post model and you will find that function that provides a human readable version of our object. Let's take a closer look at what this does. Let's go ahead and comment out the 'self.title' because we will bring that back into play. Put a string in place of that for example 'abc'. Now go into your admin site and refresh may need to give it a minute to catch up but after refresh, you will see that title has now changed to 'abc'. If we created another post it would also say abc. This is just a human-readable version of our object. The object is our Post model. Let's change it to 'seo_title' now refresh and it will return your seo_title. I hope you understand that this is just returning whatever we ask it to. Now change it back to 'self.title' this is how we will use it through our tutorial series.

In the next tutorial, we will modify and improve how the model looks in the admin site.