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
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
Now, check your root URLs file so you have this path:
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
You should be able to see the following login page
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
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
Update the code as follows
from django.contrib import admin from books.models import Book # Register your models here. admin.site.register(Book)
from books.models import Bookimports the
admin.site.register(Book)registers our model
Bookso we can access it in the admin panel
/authors/admin.py as follows
from django.contrib import admin from authors.models import Author # Register your models here. admin.site.register(Author)
/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
In the next tutorial we will add a template to our applications and try to create new pages.
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.