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

4. Introduction to Django Admin

Overview

This chapter introduces you to the basic functionality of the Django admin app. You will start by creating superuser accounts for the Bookr app, before moving on to executing Create Read Update Delete (CRUD) operations with the admin app. You will learn how to integrate your Django app with the admin app and you'll also look at the behavior of ForeignKeys in the admin app. At the end of this chapter, you will see how you can customize the admin app according to a unique set of preferences by sub-classing the AdminSite and ModelAdmin classes, to make its interface more intuitive and user-friendly.

Introduction

When developing an app, there is often a need to populate it with data and then alter that data. We have already seen in Chapter 2, Models and Migrations, how this can be done on the command line using the Python manage.py shell. In Chapter 3, URL Mapping, Views, and Templates, we learned how to develop a web form interface to our model using Django's views and templates. But neither of these approaches is ideal for administering the data from the classes in reviews/models.py. Using the shell to manage data is too technical for non-programmers and building individual web pages would be a laborious process as it would see us repeating the same view logic and very similar template features for each table in the model. Fortunately, a solution to this problem was devised in the early days of Django when it was still being developed.

Django admin is actually written as a Django app. It offers an intuitively rendered web interface to give administrative access to the...

Creating a Superuser Account

Our Bookr application has just found a new user. Her name is Alice, and she wants to start adding her reviews right away. Bob, who is already using Bookr, has just informed us that his profile seems incomplete and needs to be updated. David no longer wants to use the application and wants his account to be deleted. For security reasons, we do not want just any user performing these tasks for us. That's why we need to create a superuser with elevated privileges. Let's start by doing just that.

In Django's authorization model, a superuser is one with the Staff attribute set. We will examine this later in the chapter and learn more about this authorization model in Chapter 9, Sessions and Authentication.

We can create a superuser by using the manage.py script that we have explored in earlier chapters. Again, we need to be in the project directory when we enter it. We will use the createsuperuser subcommand by entering the following command...

CRUD Operations Using the Django Admin App

Let's get back to the requests we got from Bob, Alice, and David. As a superuser, your tasks will involve creating, updating, retrieving, and deleting various user accounts, reviews, and title names. This set of activities is collectively termed CRUD. CRUD operations are central to the behavior of the admin app. It turns out that the admin app is already aware of the models from another Django app, Authentication and Authorization – referenced in INSTALLED_APPS as 'django.contrib.auth'. When logging into http://127.0.0.1:8000/admin/, we are presented with the models from the authorization application, as shown in Figure 4.3:

Figure 4.3: The Django administration window

When the admin app is initialized, it calls its autodiscover() method to detect whether any other installed apps contain an admin module. If so, these admin models are imported. In our case, it has discovered 'django.contrib...

Registering the Reviews Model

Let's say that Carol is tasked with improving the Reviews section in Bookr; that is, only the most relevant and comprehensive reviews should be shown, and duplicate or spammy entries should be removed. For this, she will need access to the reviews model. As we have seen above with our investigation of groups and users, the admin app already contains admin pages for the models from the authentication and authorization app, but it does not yet reference the models in our Reviews app.

To make the admin app aware of the models, we need to explicitly register them with the admin app. Fortunately, we don't need to modify the admin app's code to do so as we can instead import the admin app into our project and use its API to register our models. This has already been done in the authentication and authorization app, so let's try it with our Reviews app. Our aim is to be able to use the admin app to edit the data in our reviews model.

...

Customizing the Admin Interface

When first developing an application, the convenience of the default admin interface is excellent for building a rapid prototype of the app. Indeed, for many simpler applications or projects that require minimal data maintenance, this default admin interface may be entirely adequate. However, as the application matures to the point of release, the admin interface will generally need to be customized to facilitate more intuitive use and to robustly control data, subject to user permissions. You might want to retain certain aspects of the default admin interface, and at the same time, make some tweaks to certain features to better suit your purposes. For example, you would want the publisher list to show the complete names of the publishing houses, instead of "Publisher(1), Publisher(2)…" and so on. In addition to the aesthetic appeal, this makes it easier to use and navigate through the app.

Site-Wide Django Admin Customizations

...

Summary

In this chapter, we saw how to create superusers through the Django command line and how to use them to access the admin app. After a brief tour of the admin app's basic functionality, we examined how to register our models with it to produce a CRUD interface for our data.

Then we learned how to refine this interface by modifying site-wide features. We altered how the admin app presents model data to the user by registering custom model admin classes with the admin site. This allowed us to make fine-grained changes to the representation of our models' interfaces. These modifications included customizing change list pages by adding additional columns, filters, date hierarchies, and search bars. We also modified the layout of the model admin pages by grouping and excluding fields.

This was only a very shallow dive into the functionality of the admin app. We will revisit the rich functionality of AdminSite and ModelAdmin in Chapter 10, Advanced Django Admin and...

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}