Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Web Development with Django

You're reading from  Web Development with Django

Product type Book
Published in Feb 2021
Publisher Packt
ISBN-13 9781839212505
Pages 826 pages
Edition 1st Edition
Languages
Authors (5):
Ben Shaw Ben Shaw
Profile icon Ben Shaw
Saurabh Badhwar Saurabh Badhwar
Profile icon Saurabh Badhwar
Andrew Bird Andrew Bird
Profile icon Andrew Bird
Bharath Chandra K S Bharath Chandra K S
Profile icon Bharath Chandra K S
Chris Guest Chris Guest
Profile icon Chris Guest
View More author details

Table of Contents (17) Chapters

Preface
1. Introduction to Django 2. Models and Migrations 3. URL Mapping, Views, and Templates 4. Introduction to Django Admin 5. Serving Static Files 6. Forms 7. Advanced Form Validation and Model Forms 8. Media Serving and File Uploads 9. Sessions and Authentication 10. Advanced Django Admin and Customizations 11. Advanced Templating and Class-Based Views 12. Building a REST API 13. Generating CSV, PDF, and Other Binary Files 14. Testing 15. Django Third-Party Libraries 16. Using a Frontend JavaScript Library with Django

10. Advanced Django Admin and Customizations

Overview

This chapter introduces you to advanced customizations to the Django Admin site so that you can tailor the appearance of the Django Admin dashboard to make it blend with the rest of your web project. You will see how new features and capabilities can be added to your web project's Django admin interface to make it substantially more powerful and useful for your project's goals. These customizations are driven by the addition of custom templates that help modify the look and feel of existing pages. These custom templates also add new views that can help extend the default functionalities of the Admin dashboard. Completing this chapter will equip you with skills that not only let you customize the interface, but also the functionality of your project's Django-based admin page.

Introduction

Let's say we want to customize the front page of a large organization's admin site. We want to show the health of the different systems in the organization and see any high-priority alerts that are active. If this were an internal website built on top of Django, we would need to customize it. Adding these kinds of functionalities will require the developers in the IT team to customize the default admin panel and create their own custom AdminSite module, which will render a different index page in comparison to what is provided by the default admin site. Fortunately, Django makes these kinds of customizations easy.

In this chapter, we will look at how we can leverage Django's framework and its extensibility to customize Django's default admin interface (as shown in Figure 10.1). We'll not just learn how to make the interface more personal; we will also learn how we can control the different aspects of the admin site to make Django load a custom...

Customizing the Admin Site

Django as a web framework provides a lot of customization options for building web applications. We will be using this same freedom provided by Django when we are working on building the admin application for our project.

In Chapter 4, Introduction to Django Admin, we looked at how we can use the admin.site properties to customize the elements of our Django's admin interface. But what if we require more control over how our admin site behaves? For example, let's say we wanted to use a custom template for the login page (or the logout page) to show to users whenever they visited the Bookr admin panel. In this case, the admin.site properties provided might not be enough, and we will need to build customizations that can extend the default admin site's behavior. Luckily, this can be easily achieved by extending the AdminSite class from Django's admin model. But before we jump into building our admin site, let's first understand how...

Adding Views to the Admin Site

Just like general applications inside Django, which can have multiple views associated with them, Django allows developers to add custom views to the admin site as well. This allows the developer to increase the scope of what the admin site interface can do.

The ability to add your own views to the admin site provides a lot of extensibility to the admin panel of the website, which can be leveraged for several additional use cases. For example, as we discussed at the start of the chapter, an IT team of a big organization can add a custom view to the admin site, which can then be used to both monitor the health of the different IT systems in the organization and to provide the IT team with the ability to quickly look at any urgent alerts that need to be addressed.

Now, the next question we need to answer is: How can we add a custom view to the admin site?

As it turns out, adding a new view inside the admin template is quite easy and follows...

Summary

In this chapter, we looked at how Django allows the customization of its admin site. It does so by providing easy-to-use properties for some of the more general parts of the site, such as title fields, headings, and home links. Beyond this, we learned how to build a custom admin site by leveraging the concepts of object-oriented programming in Python and creating a child class of AdminSite.

This functionality was further enhanced by implementing a custom template for the logout page. We also learned how we can supercharge our admin dashboard by adding a new set of views to allow enhanced usage of the dashboard.

As we move on to the next chapter, we will build upon what we have learned so far by learning how to create our own custom tags and filters for templates. Furthermore, using class-based views, we will gain the ability to build our views in an object-oriented style.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Web Development with Django
Published in: Feb 2021 Publisher: Packt ISBN-13: 9781839212505
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}