If you have followed along in the Django Development series, you’ve started a Django application, connected your application to MySQL, and created the database models for the
Comments data within your blog web application.
In this tutorial, we will connect to and enable the Django admin site so that you can manage your blog website. The Django admin site comes pre-built with a user interface that is designed to allow you and other trusted individuals to manage content for the website.
It is worth noting that Django’s official documentation points out that although this is ideal for an organization’s internal use, it is not recommended to build a web application around an automatically generated Django admin interface. If you find that your interface needs to be more process-centric or proves to abstract away the implementation details of database tables and fields, it would be best for you to write your own views for the admin side.
This tutorial is part of the Django Development series and is a continuation of that series.
If you have not followed along with this series, we are making the following assumptions:
As this tutorial is largely dealing with the Django Admin Interface, you may be able to follow along even if you have a somewhat different setup.
Whenever we begin doing work in Python and Django, we should activate our Python virtual environment and move into our app’s root directory. If you followed along with the series, you can achieve this by typing the following.
- cd ~/my_blog_app
- . env/bin/activate
In order to enable the Django Admin, we need to ensure that our app is part of the list of
INSTALLED_APPS in the
Navigate to the directory of the settings file:
- cd ~/my_blog_app/blog/blog/
From here, open the
settings.py file. If it’s not already there, add
django.contrib.admin to the list of
INSTALLED_APPS, using a text editor like nano.
- nano settings.py
INSTALLED_APPS section of the file should be similar to the file below. Our app in the list is the one on the top,
'blogsite', but if you created an app of a different name, ensure that that app is listed in this file as demonstrated.
... # Application definition INSTALLED_APPS = [ 'blogsite', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] ...
Be sure to save and close the file if you made changes. In nano, you can do this by typing
Y and then
We can now open the
urls.py file, again with nano or another text editor.
- nano urls.py
Under the comment at the top, the file should resemble the following.
… """ from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ]
If the file is different from what is above, copy and paste the lines above into your
Now that we have ensured that our Django web project has the appropriate code in the
urls.py files, we know our application will have access to the admin models and admin user interface.
We should next migrate the models to the database so that it picks up the newly added Admin models.
Navigate to the directory where the
manage.py file is located.
- cd ~/my_blog_app/blog
Remember to run the
migrate command whenever you make any changes to the
models, like so.
- python manage.py migrate
If we did not make any changes to the files above, we should receive output similar to the following upon running the
OutputOperations to perform: Apply all migrations: admin, auth, blogsite, contenttypes, sessions Running migrations: No migrations to apply.
Otherwise, the output should indicate that Django made the migrations needed to support our app.
We can now start the server by running the following command. You can replace
0.0.0.0 with your IP address.
- python manage.py runserver 0.0.0.0:8000
Then navigate to the admin panel’s URL in a browser of your choice. Be sure to input your server’s IP address.
You will receive a login screen similar to this.
Getting to this screen lets us know that we have successfully enabled the admin app.
Though we have enabled the app, we may not have set up a Django administration account yet. We can create the admin account in order to login in the next step.
If you already have set up an admin account and can log into your admin page, you can skip this step.
Open a new terminal to connect to the server, or disable the Django app by pressing
C so that we can work on our server terminal’s programming environment.
Django allows you to generate a super-user account, which we can do by running the
manage.py file to start the super-user creation process.
- python manage.py createsuperuser
Once we do so, we’ll be prompted to fill in details for our username, email, and password. In this tutorial, we’ll make an admin account with the username
admin_user, the email
email@example.com and the password
admin123. You should fill this information in with your own preferences and be sure to use a secure password that you’ll remember.
OutputUsername (leave blank to use 'root'): admin_user Email address: firstname.lastname@example.org
Then put in your password twice when you see the
Password: prompt. You will not receive output from the keystrokes of your password when you enter it. Press enter after each prompt to confirm your password.
OutputPassword: Password (again):
At this point, we now have an admin account with the username
admin_user and the password
Let’s log in and investigate what exists on our admin page.
If needed, run the Django app again with
python manage.py runserver 0.0.0.0:8000 and then navigate once more to the URL
http://your-server-ip:8000/admin/ to get to the admin login page. Then log in with the username and password and password you just created.
After a successful login, you’ll receive the following page.
Next, we will need to work on connecting our blog app to the admin panel.
In the previous step, we successfully logged into the admin interface, but you may have noticed that our blog app is not yet available there. To populate our admin interface with the blog app, we need to add and register it with the associated models
To do this, we’ll create an empty file called
urls.py, in the
blogsite directory, like so:
- touch ~/my_blog_app/blog/blogsite/urls.py
In this file, we will add the URL pattern for our blog application so that we can access it via the admin interface.
Navigate to the location of that
urls.py file we’ve just created.
- cd ~/my_blog_app/blog/blogsite/
Then open the file with nano, for instance.
- nano urls.py
Add the following lines of code to the file.
from django.urls import path from . import views urlpatterns = [ path('$/', views.posts, name='posts'), path('$/', views.comments, name='comments'), ]
These are the URL pattern expressions needed to allow our application to access the
Comments. We have not created those
views yet, but will cover this later on in the series.
Connecting our blog to the admin interface will allow us to see links for both the
Comments inside the admin dashboard. Right now, the dashboard currently just displays links for
To connect the two together, we need to register our
Comments models inside of the admin file of
Navigate to the
- cd ~/my_blog_app/blog/blogsite
Then, open the
admin.py file in a text editor of your choice.
- nano admin.py
The file will be populated with an import statement and a comment.
from django.contrib import admin # Register your models here.
You should edit the file so that it contains the following code in order to support our app.
from django.contrib import admin from blogsite.models import Post from blogsite.models import Comment admin.site.register(Post) admin.site.register(Comment)
When you are satisfied with the file, save and exit.
You have now registered the
Comment models inside of the admin panel. This will enable the admin interface to pick these models up and show them to the user that is logged into and viewing the admin dashboard.
Now that you’ve added the relevant Python code, run the server. Open
http://your-server-ip:8000/admin and log in to the admin using your credentials if you’re not logged in already. In this tutorial, we’ve been logging in with the username
admin_user and password
Now that you’ve logged in, you should be served the following webpage. If it has not changed from before, you may need to refresh your browser.
This verifies that we have now connected our app,
blogsite, to the Django admin dashboard.
When you are done with testing your app, you can press
C to stop running the Django server. This will return you to your programming environment.
When you are ready to leave your Python environment, you can run the
Deactivating your programming environment will put you back to the terminal command prompt.
In this tutorial, you have successfully enabled the admin interface, created an admin login, and registered the
Comment models with the admin.
The Django admin interface is how you will be able to create posts and monitor comments with your blog.
Coming up in the series, we will be creating the
views for the blog application.
If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.
Django is a free and open-source web framework written in Python. Django’s core principles are scalability, re-usability and rapid development. It is also known for its framework-level consistency and loose coupling, allowing for individual components to be independent of one another. Don’t repeat yourself (DRY programming) is an integral part of Django principles.