Home > Articles > Programming > Python

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Trying Out the admin

Now that we’ve set up our Django site with the admin app and registered our model with it, we can take it for a spin. Issue the manage.py runserver command again. Now, go to in your Web browser. (Don’t worry if your dev server address is different; just add an admin/ onto it, whatever it is.) You should see a login screen, as shown in Figure 2.4.

Figure 2.4

Figure 2.4 The admin login screen

Type the “superuser” name and password you created earlier. Once you’ve logged in, you see the admin home page, as shown in Figure 2.5.

Figure 2.5

Figure 2.5 The admin home page

We’ll tour this interface later in the book; for now, just confirm your application, Blog, is showing up as seen in the screenshot. If it’s not, recheck the previous steps.

What’s a blog without content? Click the Add button to the right of Blog Posts. The admin presents a form for adding a new post, as shown in Figure 2.6.

Figure 2.6

Figure 2.6 Adding new content via the admin

Give your post a title and some scintillating content. For the timestamp, you can click the Today and Now shortcut links to fill in the current date and time. You can also click the calendar or clock icons to pull up handy date and time pickers.

When you’re done writing your masterpiece, click the Save button. You see a screen with a confirmation message (“The blog post ‘BlogPost object’ was added successfully”) and a list of all your blog posts—a grand total of one at this point, as shown in Figure 2.7.

Figure 2.7

Figure 2.7 Successfully saving your first blog entry

Why is the post given the awkward name of “BlogPost object”? Django is designed to flexibly handle an infinite variety of content types, so it doesn’t take guesses about what field can be the best handle for a given piece of content. Throughout Part 3’s example applications, you see examples of defining how to specify a particular field, or specially constructed string, to be used for your objects’ default labels.

Now go ahead and add a second post with different content by clicking on the Add Blog Post + button to the upper-right. When you are returned to the list view, you just see another BlogPost row added to the page. If you refresh the page or go away and come back to your application, the output has not improved any—you just do not feel satisfied with seeing all the entries generically labeled as “BlogPost object,” as shown in Figure 2.8. You are not alone if you’re thinking, “There has got to be a way to make it look more useful!”

Figure 2.8

Figure 2.8 Not the most useful summary page

However, we don’t have to wait until then to clean up the list display in our admin view. Previously, we enabled the admin tool with the bare minimum of configuration, namely registering our model with the admin app all by itself. However, with an extra two lines of code and a modification of the registration call, we can make the presentation of the listing much nicer and more usable. Update your mysite/blog/models.py file with a new BlogPostAdmin class and add it to the registration line, so your models.py looks like this:

from django.db import models
from django.contrib import admin

class BlogPost(models.Model):
    title = models.CharField(max_length=150)
    body = models.TextField()
    timestamp = models.DateTimeField()

class BlogPostAdmin(admin.ModelAdmin):
    list_display = ('title', 'timestamp')

admin.site.register(BlogPost, BlogPostAdmin)

The development server notices your changes and reloads your models file. If you are monitoring your command shell, you see some output to this effect.

If you refresh the page, you now see much more useful output based on the new list_display variable you added to your BlogPostAdmin class (see Figure 2.9).

Figure 2.9

Figure 2.9 Much better

Try clicking on the Title and Timestamp column headers that have appeared—each one affects how your items are sorted. For example, click once on Title to sort in ascending order by title; click the Title header a second time to change to descending order.

The admin has many other useful features that can be activated with just a line or two of code: searching, custom ordering, filters, and more. As we’ve mentioned a few times already, Parts III and IV cover or demonstrate many of these topics in greater detail.

  • + Share This
  • 🔖 Save To Your Account