The Django admin app

Now that we have looked at what new features were used in the code pack, let's move on to the main topic for this chapter—the Django admin app. The admin app is quite possibly one of the main reasons for the popularity of Django over other similar web frameworks. It is the embodiment of the batteries included nature of Django. With minimal configuration, the admin app provides a fully featured and extremely tailored CMS, enough to rival big names such as WordPress and Drupal.

In this chapter, you will learn how easy it is to configure and customize the admin to get most of the functionality that you will want in the admin panels of your web apps. Let's start by fixing the most immediate problem for our fictional client, a car rental business owner, which is the ability to add and edit car details.

When you start a new application, Django by default creates an admin.py file in the application folder. Change the frontend/admin.py file in our project to match this content:

from django.contrib import admin
from frontend.models import Car
admin.site.register(Car)

That's it. Really! Three lines in total and you get the ability to edit and add Car objects to your database. That's the power of Django, right there in those three lines. Let's test it out. In your browser, visit http://127.0.0.1:8000/admin and you should see a page similar to the following one:

The Django admin app

Tip

Don't worry if your admin looks slightly different. Django updates the theme for the admin every once in a while and, depending on which version of Django you are using, your admin may look slightly different. However, all the functionality will be there and will almost always have the same interface layout.

Oops, there is one thing that we left out. We didn't create a user to log in with. This is easily fixable. In the command line, run this command with the virtual environment activated:

> python manage.py createsuperuser

Just follow along with the prompts to create a new user. Once you have created the user, use it to log in to the admin. After logging in, you should see something similar to this:

The Django admin app

A few things to note in this screen. First, Django will by default add links to manage Groups and Users. Secondly, any models that we configure to show up in the admin are grouped by their application name. Thus, the link to manage Cars shows up under the label for the app that defines the model, Frontend.

Tip

If you pay close attention, you might note that the admin lists the plural name of our Car model. How does it know the plural name? Well, it simply adds an 's' in front of our model name. In a lot of cases, this doesn't work, for instance, if we had a model called Bus. For cases like this, Django allows us to configure the plural name for a model.

Let's try editing one of the car objects that we have in our database. Click on the Cars link and you should see a screen similar to the following one:

The Django admin app

The list doesn't look very useful. We have no idea which car object is which. We'll fix this in a bit. For now, just click on the top car object in the list, and you should see a page where you can edit the details for that object:

The Django admin app

Note

The Django admin docs refer to this list as changelist. I will just call it the list view in this chapter.

Let's change the name of the car. I changed Dodge Charger to My New Car Name. Scroll to the bottom of the page after changing the name and click on save. To make sure that our changes were actually saved, open the home page for our app at http://127.0.0.1:8000/ and you'll see that the car you edited will have the new name displayed.

Let's try something more complicated—adding a new car! Click on the ADD CAR button on the right-hand side of the screen and fill in the details however you want. Just be sure to select the is_available checkbox; otherwise, the new car won't show up on the home page. I filled up the form as shown in this screenshot:

The Django admin app

I also downloaded an image for the car from Google Images and selected it for the Image field. Click on the save button and visit the home page again. The new car you added should show up at the end of the list:

The Django admin app

As I mentioned at the start of this section, the power of the Django admin is one of the main reasons for Django's popularity. By now you should see why. In three lines, we have a complete and working, albeit not very pretty, content management system that the client can use to edit and add cars to their site.

However, in its current form, the admin looks like a quick hack job. The client will probably not be very happy with this. They can't even see which car they are about to edit before the edit page opens up. Let's fix this first. We will come back to the code we just wrote for the admin in a bit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.134.17