First Look to Django Admin Panel
By: Berk Berber
Tags: Backend, Django Rest Framework, Django, Python

One of the best part of Django is having an automatic admin panel with user interface. With this admin panel, user who has permission can create new records or view, update and delete all records in database.

When we create a project all the configuration required to include admin panel in our website will be done automatically.

This feature can save us a lot of time during development. Because it can make very easy to testing our models and data. Also, there are many ways to customize this admin panel. We can change ordering of models and we can change which columns of model will be visible in list. We can give different permissions to different users who has access to admin panel. These are only a few examples for what we can do with Django admin panel.

Accessing Admin Panel

As a default, django is using /admin url for admin panel. To access the admin panel, first we need to run our project.

Note: If you don’t know how to run server or how to create project in Django, I recommend you to read my article called as Creating First Django Project.

Firstly, let’s open our urls.py file. This is the file which contains project level URL routings. When we create new web pages or APIs, we need to add these URLs to urls.py for routing.

Django is using apps for making project structure better. I will talk about apps in Django in another article. But if we add URLs in app level urls.py file, we need to include them in our project level urls.py file.

Now, take a look at project level urls.py file.

As you see in the above picture, I have marked line 20 with red rectangle. Here, first parameter says what is our URL and second parameter says that this url will be the admin panel of our project.

After running our server successfully, let’s open our browser and go to this website http://127.0.0.1:8000/admin

Note: If you run your project in different url, don’t forget to change this area.

When we go to our admin page in browser, we will see this form. To access admin panel, we should have super user. But for now, we don’t have any super user and we need to create new super user.

Creating new super user

To create new super user, we will use command line. We can run following command:

python manage.py createsuperuser

If we run the our project first time and didn’t make any migration on database, we will get an error as shown in the below:

As I marked in red rectangle, error message gives us some hint about the error. The error caused because we have unapplied migrations on database.

To fix that problem, we can run following command.

python manage.py migrate

With this command, python will do all migrations and give outputs about them to us. If everything is fine, that means we are ready create our first super user.

Let’s run the our “python manage.py createsuperuser” again and let’s see what is going to happen.

Python will ask us 3 information. These are username, email address and password. We need to fill these information form which is about us. We will access the admin panel with username and password which we will define in this form.

Now let’s go to “http://127.0.0.1:8000/admin" url and login with our username-password.