Sometimes the admin site works great out of the box. Other times, you need to add some custom code to certain parts of your admin site.
Maybe, you want some custom tables that shows your revenue for the month (i.e. you have an e-commerce site), maybe you’d like to create a button so you could upload batch SKUs for product inventory in your e-commerce site.
The sky really is the limit. This post will show you not only how to update and custom the admin templates, but it will also give you a clue to how to update the actual admin views so you can change the behavior of the entire admin interface.
Step 1: Override Admin Templates
Create a an admin directory in your project’s templates directory. Create a subdirectory inside of the admin directory that will be the name of your app (i.e. my_app).
You have some options that you could edit for specific apps:
All the other ones, you would just copy into your newly created admin directory.
Step 2: Override Admin View by Subclassing AdminSite
If you’d like to change how your Admin site behaves, you’ll need to subclass
from django.contrib.admin import AdminSite from .models import MyModel class MyAdminSite(AdminSite): site_header = "My Super Awesome Customized Admin Site" # More stuff including view methods that you can also override.
Any method or attribute you find here can be overridden. You can literally change how the entire Admin site behaves and works.
Step 3: Instantiate your Admin Class and Register your Models on it
Once your admin site works the way you want it to, you can instantiate and register your models to it:
admin_site = MyAdminSite(name="myadmin") admin_site.register(MyModel)
Now, you can add your admin to your urlconf:
from django.conf.urls import url from my_app.admin import admin_site urlpatterns = [ url(r'^myadmin/', admin_site.urls), ]
Now, you can change the look and feel of you admin site, and even update the behavior of the entire admin site. What would you like to add to your site?
What do you do after the Official Django Tutorial?
Here's what you'll learn:
- Start writing tests
- Increase the size / complexity of the Polls application.
- Learn how to code in Django by reading the Django source code.
- Finding code snippets to use in your website by reading the code of other websites.
- Deploying on Heroku and AWS.