Basics Customization
Learn about basic design customization and how to create a custom admin class.
We'll cover the following
Design customization
You will now focus on design. The Django admin lets you override its HTML templates for you to add your own custom elements or even rebrand the full design of the interface.
You can begin with simple things.
You can change the site title and index title easily by configuring the following in urls.py
:
admin.site.site_header = "Django Admin Ultimate Guide"
admin.site.site_title = "Django Admin Title"
admin.site.index_title = "Welcome to Ultimate Guide"
site_header
: The text placed at the top of each admin page, as an <h1> (a string). By default, this is “Django administration”.site_title
: The text found at the end of each admin page’s <title> (a string). By default, this is “Django site admin”.index_title
: The text positioned at the top of the admin index page (a string). By default, this is “Site administration”.
To remove some models from the admin, you can unregister them in sample_app/admin.py
.
admin.site.unregister(User)
admin.site.unregister(Group)
And now, you will see that the Authentification menu has been removed.
Customising admin class
You can also create your own Admin
class inside sample_app/admin.py
:
from django.contrib.admin import AdminSite
## Custom admin site
class MyUltimateAdminSite(AdminSite):
site_header = 'My Django Admin Ultimate Guide'
site_title = 'My Django Admin Ultimate Guide Administration'
index_title = 'Welcome to my "sample_app"'
site = MyUltimateAdminSite()
Then in helloworld/urls.py
, you need to adapt your code to use your new admin class.
from sample_app.admin import site
admin.site = site
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
]
Finally, you need to explicitly register your models to your new admin in sample_app/admin.py
.
site.register(Author,AuthorAdmin)
site.register(Question,QuestionAdmin)
site.register(Choice,ChoiceAdmin)
site.register(Group)
site.register(User)
Be careful because the previous method
@admin.register
will not work anymore.
Using a custom class will allow you to do more functionalities with the templates customization, as you will see in the Advanced Customization section of the course.
Try it yourself
In the code widget below, try out the things you learned in this lesson:
- Try custom names for
site_header
,site_title
andindex_title
. - Unregister the group and user models.
- Create a custom admin class and register your models with this new custom class.
How to see the output: To see the output of your code after making any changes, click the Run button and open the link provided below the code widget in a new browser tab, or switch to the Output tab of the widget. To log in to the Django Admin page, use the following credentials:
Username
: educativePassword
: edu1234
Get hands-on with 1400+ tech skills courses.