In this Django Tutorial we are going to talk about Django Super User, so it is one of the most powerful features that Django has automatic admin interface, It can reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site.
Now first of all you need to create a super user in django, you can use this command for creating django super user.
1 |
python manage.py createsuperuser |
After that it will asks you for username, email and password.
Now got to http://localhost:8000/admin/, give your username and password and now we have our admin site.
All right guys now we want to add our django model to our admin site, there are two ways that you can register your django models in the admin site, first of all open your admin.py file and add this code.
1 2 3 4 5 6 |
from django.contrib import admin from .models import Question # Register your models here. admin.site.register(Question) |
Now if you check your Django Admin Site you will see your model in their and you can simply do CRUD operations using you django admin panel.
In the second way you can customize your django admin site, in this way first we register our model using register decorator, after that we create a class that extends from ModelAdmin and at the end you can add your filters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from django.contrib import admin from .models import Question # Register your models here. #first way #admin.site.register(Question) #second way @admin.register(Question) class QuestionModel(admin.ModelAdmin): list_filter = ('text', 'pub_date') list_display = ('text', 'pub_date') date_hierarchy = ('pub_date') ordering = ('pub_date', 'text') |
Now if you check your admin site, you will have a customized admin panel.