Lesson 5: Creating Administration Site for Our Blog Models in Django 1.11.4 and Python 3.5

Welcome to this lesson, great to have you on board. My name is Henry and I am excited to continue with this tutorial series where we will be building blog application using Django 1.11.4 and Python 3.5. We are in our fifth lesson, creating an administration site for our blog Post model. In the previous lesson, we designed blog Post data schema in models.py file.

In this lesson, we will first create a super user and then add the Post model to the administration site, further we will customize how models are displayed. Now that we have defined the Post model, we will create a simple administration site to manage blog post. Django comes with a built-in administration interface which is very useful for editing and deleting content.

Django administration site is created by developers who have years of experience and this makes it a very reliable application. The Django admin site is built dynamically by reading your models meta data and providing a production-ready interface for managing your content. You can use Django admin site out of the box by configuring how you want your models to be displayed in it. Remember that django.contrib.admin is already included in the installed app settings in our project and that’s why we don’t have to add it. Let’s create a super user.

By now you should be able to activate your virtual environment, it’s important to always activate your virtual environment. I am going to activate my virtual environment and navigate to my project base directory, Django project base directory is where manage.py is located. To create superuser, run the following command: python manage.py createsuperuser your terminal will ask you to enter your Username, Email address and Password. Here is my screen-shot of my terminal:

Now we will deal with Django administration site, start the development server by running the following command: python manage.py runserver here is a screen-shot of my terminal showing development server up and running.

On your browser, navigate to this link: http://localhost:8000/admin/ you should see the administration login screen as shown below:

Login using the credentials you created when you created superuser. You will see the admin index page as shown here:


 The Groups and User model you see in the image above are part of the Django authentication framework, located in django.contrib.auth if you click on user you will see the super user you created. The post model of our application has a relationship with Django user model, remember it’s a relation defined by the author field in our Post model.

To add our Post model to the administration site, let’s open our project in IDE. Navigate to blog app and open admin.py file. Make sure the admin.py file has the following code:

Now reload the admin site in your browser and you should see the following screen-shot:

Now the blog post model is accessible in our admin site, that was easy right. This is just but a small power of what Django admin site can do. When you register your model in Django admin site, you get a user friendly interface generated by introspecting your models that allows you to list, edit, create and delete object in a simple way.

Click on the add link on the right of the Posts to add a new post, you will see the create form that Django has generated dynamically based on your Post model. Here is a screen-shot:

Fill in the form and click the save button. You should be redirected to post list page with a successful message on the post you just created. Django uses different form widgets for each type of fields, even complex fields such as date time fields that are displayed with an easy interface like a JavaScript date picker.

Let’s customize the admin site, for that. Add the following code in the admin.py file of your blog application.

In the code above, we are telling Django admin site that our Post model is registered into the admin site using a custom class on line 4 that inherits from admin.ModelAdmin In this class, we can include information on how to display the models and interact with it on the admin site. The list_display attribute allows you to set the fields of your model that you want to display on the admin site page. To learn more on how to customize your admin site, visit Django admin. Now go back to your browser and reload the post list page. Now it will look like the screen-shot below:

The list page now includes a right sidebar that allows you to filter the post based on the fields included in list_filter attribute on line 6. A search bar has appeared on the page due to search_fields attribute on line 7. Just below the search field, there is a bar to navigate through a date hierarchy, this has been defined by the date_hierarchy attribute on line 10.

With a few line of code, we have customized the way our Post model is displayed in the admin site. There are plenty of ways to customize and extends the Django administration site. Nice, we have done a great job.

Goals achieved in this lesson

  • We learned how to create superuser.

  • We learned how to login to Django administration site.

  • We added our Post model to Django admin site.

  • We customized the way our Post model is displayed in the admin site.

With that we conclude our fifth lesson, to get the code visit building a blog application. See you in lesson 6.

Facebook Comments
Get free email updates!
Signup now and receive an email once we publish new content.
We respect your privacy

Henry Mbugua

I am a Junior Developer ready to enter major leagues. My professional ambition is to stay versatile and be able to fill different roles within a product group. As such, I have been learning technology across frontend and backend.