Enabling and Connecting the Django Admin Interface featured image

Enabling and Connecting the Django Admin Interface

Django is a popular, free, and open-source web application framework built on Python. It strives to streamline web app development. Django comes with a solid set of components to develop websites faster and easier.

In our series of articles on Django, we have been exploring how to use Django with a demo app. In this guide, we will build on what we learned in the Creating Django Models: A Tutorial guide and connect our Django model with the admin interface.

Let’s start!

Prerequisites

This guide goes deeper into configuring and building the Django app. It requires the following prerequisites:

If you’ve followed the series so far, we’ve created a sample Django app, established a connection with MySQL, and created database models for Posts and Comments. The models feature the simple functionalities of a basic blog site. This guide will continue from here on. Note that Django official documentation discourages building web apps around an automatically-generated Django admin interface.

Step 1. Enabling the Admin

As demonstrated in the Prerequisites tutorials, it’s always recommended to build your Django app in a dedicated virtual Python environment. Whenever we’re intending to work on the app, you have to activate the virtual environment.

First, run the activation script of the Python virtual environment:

Enabling and Connecting the Django Admin Interface Activate Env

We need to tweak the Django app configuration first. Open up the configuration file settings.py in a text editor:

Scroll down to the section INSTALLED_APPS. It lists all the apps connected to our Django project. Alongside the default apps, it should contain the Django model app random_app and django.contrib.admin:

Enabling and Connecting the Django Admin Interface Installed Apps

Next, we need to tweak the URL configuration file urls.py. Open the file in a text editor:

The file should contain the following codes:

Enabling and Connecting the Django Admin Interface Sample App URLs

Save the file and close the editor. These tweaks ensure that our Django app will have access to the admin models and the admin user interface.

Step 2. Verifying the Admin Installation

After making tweaks to the configuration, it’s recommended to migrate the models so that the database can update with the Admin models.

Run the database migration on our Django app:

Enabling and Connecting the Django Admin Interface Run Migration

We’re now ready to launch the Django server. Because it’s a local project, we are going to launch the server locally. The server should run on port 8000. If you have a firewall configured, it must allow traffic exchange for port 8000. This in-depth guide on UFW showcases how to allow traffic to a certain port.

Launch the server:

Enabling and Connecting the Django Admin Interface Launch Server

After that, access the server URL on a web browser:

Enabling and Connecting the Django Admin Interface Server Webpage

To access the admin access page, go to the following URL:

Enabling and Connecting the Django Admin Interface Admin Page

We need an admin account ready for our Django app. This admin account will have the highest priority on our app.

Step 3. Creating the Superuser Account

So far, we’ve verified that our Django app is successfully using the Adminsmodel and the admin page is accessible. Turn off the Django server by pressing Ctrl + C.

Once shut down, we can make changes to the Django app. Run the following command to create a superuser/admin account:

Enabling and Connecting the Django Admin Interface Create SuperUser

It will ask for the username, email, and password for verification. We can now try to access the Django admin panel. Start the Django server, and go to the admin page:

Use the username and password of the superuser to log in:

Admin Console

Voila! We’ve successfully landed on the admin panel. All that’s left is connecting the Post and Comment parts to the admin panel.

Step 4. Creating URL Patterns

To populate our admin interface with Post and Comment, we need Django to generate the associated URLs. In this section, we’ll have a look at configuring dedicated URLs for them.

Create a file urls.py under the random_app directory:

Create urls.py

Open the file in a text editor:

Next, add the following codes in the file:

URLS.py

The code describes the URL pattern for our app to access the views for Posts and Comments.

Step 5. Connecting the App to Admin

Our goal is to successfully connect the Posts and Comments to the admin panel. If the process is successful, we can see links for them on the admin dashboard.

To connect, navigate to the location of the models and open admin.py in a text editor:

Edit admin.py

Enter the following codes in the file:

Edited admin.py

Save the file and close the editor. Now, the admin panel should recognize and show links to the models.

Step 6. Verifying the Changes

Our Django app is now successfully configured to manage Posts and Comments directly from the admin panel. Start the server on localhost, log in to the admin panel, and verify the change:

Site_Admin

Once done with testing, we can safely stop the Django server and exit the virtual environment. From the console window, press Ctrl + C to stop the Django server. To exit the virtual window, run the following command:

Deactivate Virtual Env

It should return you to the normal terminal session.

Final Thoughts

In this guide, we showcased how to enable the admin interface, create an admin account, and register our custom models ( Posts and Comments) with the admin. The Django admin interface will now have improved managing posts and comments in your blog.

Here are more resources from our Django series, that will help you utilize Django with containers:

Happy Computing!