Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

Django Admin Interface

How can we populate our database before writing views? We have no forms or views for creating model objects yet. What is the alternative way? Writing SQL code is one way but we can do the easy way: Using Django Admin Panel

Django admin is a Django default app which allows the administrator to handle all the data operations such as add new data, modify it, etc.

Topics to be covered

We will cover the following topics in this lesson

  • How to create the super user
  • How to login to the admin panel
  • How to register your apps in the admin panel
  • Summary

How to create the super user

If you remember, we had some migrations by default. They belong to the Django auth app. If you check the database, we have auth tables. It means user models are handled automatically. A superuser is a user with privilege more than a regular user. They can log in to admin and manipulate the data. They can remove or add other users, etc.

We can create a superuser by running the following command

python manage.py createsuperuser

The above command will prompt you to asnwer some questions like shown in the image below

Django createsuperuser

Now, check your root URLs file so you have this path:

path('admin/', admin.site.urls),

You can now run the following command to start the built-in server

python manage.py runserver

How to login to the admin panel

Next we need to login to the admin panel by loading the following URL

http://localhost:8000/admin

You should be able to see the following login page

Django Admin Login

Enter the username root and the password that you defined when creating the super user account.

You should be able to see the following page

Django Admin Panel

As you can see from the above page, we can see the user groups and users. We can also add user groups and users.

How to register your apps in the admin panel

But our models in books are not here. We can create users but not books, authors or categories. That’s because we need to register our models in the app to the admin first.

So, go to the books app and open the file admin.py.

Update the code as follows

from django.contrib import admin
from books.models import Book

# Register your models here.
admin.site.register(Book)

HERE,

  • from books.models import Book imports the Book model
  • admin.site.register(Book) registers our model Book so we can access it in the admin panel

Update /authors/admin.py as follows

from django.contrib import admin
from authors.models import Author

# Register your models here.
admin.site.register(Author)

Now, update /categories/admin.py as follows

from django.contrib import admin
from categories.models import Category

# Register your models here.
admin.site.register(Category)

Go back to admin, you can see these models. You can create new model objects and save them to the database. Try it yourself.

The following images show you what you should be able to see and do in the admin panel

Django Admin Register Apps

 

In the next tutorial we will add a template to our applications and try to create new pages.

Summary

Djano admin panel is an app that comes with Django that saves us the time of building an admin panel to manage the data in the application. You can register your apps using the admin.py file and the necessary forms and lists will be created based on the model definition that has been registered.

Warning: Django admin has lots of abilities to manipulate your data. It is Dangerous to use it in production. So, please DO NOT USE IT IN PRODUCTION.

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

Django Database Queries

Django Templates

Leave A Reply