Django Admin Setup

Click to share! ⬇️

Django Admin Setup

We’ve made a lot of progress getting started with Django building our small Dog website. We’ve learned about getting Django setup, and creating some basic web pages that display information about Dogs in our system. Currently, the only way to interact with the data for our website is via a web browser on the front end of the site. Django has an administrative backend that is pre-built for you. In order to make use of it, you do need to set a few things up. In this tutorial, we’ll see how to configure the admin back end and how to get a superuser set up so that we can also view the Dogs in the admin panel.


Creating A Django Super User

Even if you figure out how to turn on the admin panel in Django, you won’t be able to use it until you set up a superuser. The documentation is pretty clear on how to do this, so we’ll give it a shot now. From the terminal, you’ll first need to stop the built-in server if it is already running. Once complete, we’ll type the following commands.

djangotutorial $python manage.py createsuperuser
Username (leave blank to use 'myusername'): admin
Email address: admin@example.com
Password:
Password (again):
Error: Blank passwords aren't allowed.
Password:
Password (again):
Superuser created successfully.

We now have a superuser created to use the admin panel. Note that we tried to use a blank password for demonstration purposes just to show that you need to actually provide a valid password in order for the command to work.


Configure urls.py

Now we need to set up the routing to allow us to enter a particular url in a web browser, and view a login form for the administrative interface. The code that allows that to happen is highlighted below. Ignore the code for the debug bar, that is a different topic we might cover in a future tutorial.

djangotutorial/urls.py

from django.contrib import admin
from django.urls import include, path
from djangodogs import views
import debug_toolbar

urlpatterns = [
    path('__debug__/', include(debug_toolbar.urls)),
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('dogs/<id>/', views.dog_detail, name='dog_detail'),
]

At the top of the urls.py file is the import statement that makes the admin module available to the code in this python file. Line 8 is a new entry for the admin route. What this route says is that when you visit http://localhost:8000/admin you should see a login form for the admin panel.

django admin login page

Pretty sweet! That’s the power of Python and Django. A simple import statement gives you access to a whole lot of functionality. Now let’s go ahead and log in and have a look around. Once we log in, we can see some basic options available to us. We see a welcome message of “Welcome, Admin”, a link to our main website, a change password link, and an option to log out.

django administration groups users

By clicking on the Users link, we are able to drill down and see the users in the system. Currently, there is only one present, and that is the superuser that we created via the shell command just above.

django admin super user


Register an App for Admin Panel

Right now, we don’t yet see any information regarding the djangodogs application that we built in our Django for beginners tutorial. This is because you need to register any application that you want to be able to manage in the admin back end. When you create a new app in Django using the python manage.py startapp command, one of the files that are created is admin.py. In this file, we need to register our Dog model so that we can interact with that Django model.

djangodogs/admin.py

from django.contrib import admin

from .models import Dog

admin.site.register(Dog)

With this code in place, we can now see and interact with our Dog Model.

Django administration register app

Viewing App Models

Recall that we had 3 Dogs so far in our database. If we click on the Dogs link in the admin panel, sure enough, we see three dog objects. We have full CRUD (create, read, update, delete) ability with the Model.

django view model in admin panel

Edit A Model

By clicking on one of the records from the database, we are presented with a pre-populated form where we can edit the Model however we like.

django update a model

Add A Model

Let’s use the admin panel to add another Dog to our system!

django add new entry to database admin panel
django add new entry to database success

Now when we visit the front end of the website, we can see Max is indeed part of our list of Dogs now.

new dog added to system

Delete A Record via Admin Panel

The admin panel in Django makes it easy to delete a record from the database if you choose. We aren’t going to actually delete and Dogs from our database, as no Dog should ever be deleted, but this screenshot shows how you can delete!

django admin delete database record

Learn More About Django Admin Dashboards

Django Admin Setup Summary

As we saw in this tutorial, it is easy to add an administrative interface for our project, so that admin users can see and edit data. In order to do this, we had to create a superuser using manage.py. Once that was complete, we configured both the urls.py for the project and the admin.py for any application that we want to also be displayed in the admin interface.

Click to share! ⬇️