WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[prev]

Django for the Impatient: Blog Building Basics [con't]

Java Developer (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Setting Up the Automatic admin Application

The automatic back-end application, or admin, has been described as Django's "crown jewel." For anyone who has tired of creating simple "CRUD" (Create, Read, Update, Delete) interfaces for Web applications, it's a godsend.We get much deeper into the admin in "Customizing the Admin"in Chapter 11,"Advanced Django Programming."For now, let's just turn it on and poke around.

Because it's an optional part of Django, you need to specify in your settings.py file you're using it—just like you did with your own blog app. Open settings.py and add the following line to the INSTALLED_APPS tuple, just underneath 'django.contrib.auth'.

Every time you add a new application to your project, you should run the syncdb command to make sure the tables it needs have been created in your database. Here we can see adding the admin app to INSTALLED_APPS and running syncdb triggers the creation of one more table in our database:

Now that the app is set up, all we need to do is give it a URL so we can get to it. You should have noticed these two lines in your automatically generated urls.py.

Remove the # character from the second line (and you can remove the first, comment-only line at the same time) and save the file. You've told Django to load up the default admin site, which is a special object used by the contrib admin application.

Finally, your applications need to tell Django which models should show up for editing in the admin screens. To do so, you simply need to define the default admin site mentioned previously and register your BlogPost model with it. Open the mysite/blog/models.py file, make sure the admin application is imported, and then add a line registering your model at the bottom.

This simple use of the admin is the tip of the iceberg; it's possible to specify many different admin-related options by making a special Admin class for a given model, and then registering the model with that class.We do this shortly, and you also see examples of advanced admin use in later chapters,especially in Parts III,"Django Applications by Example,"and IV,"Advanced Django Techniques and Features."

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 http://127.0.0.1:8000/admin/ 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.

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.

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.

Tip: The three most common causes for "My app doesn't show up in the admin," problems are 1) forgetting to register your model class with admin.site.register, 2) errors in the app's models.py, and 3) forgetting to add the app to the INSTALLED_APPS tuple in your settings.py file.

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.

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.

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!"

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:

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.

You may also enjoy:

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).

Try clicking on theTitle andTimestamp column headers that have appeared—each one affects how your items are sorted.For example,click once onTitle to sort in ascending orderbytitle;click theTitleheaderasecondtimetochangeto 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.

Python Web Development with Django

This chapter is an excerpt from the book, Python Web Development with Django by Jeff Forcier, Paul Bissex and Wesley Chun, published by Pearson Education, Inc., ISBN 0132356139, Copyright 2008, Pearson Education, Inc. All rights reserved.

Original: February 11, 2009

Social Bookmark


[prev]

Recent Articles

WebReference.com site name
Search Engine Optimization: Selecting and Embedding Keywords
Are Google's Language Translation Web Services Ready for Prime Time?
Installing and Using Meeplace, the Business Review CMS
internet.com site name
IBM DB2 10 for z/OS: Justifying the Upgrade
Living La Vida Colo: Choosing the Right Colocation Facility
FTC Concerns over Social Media Privacy Linger



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers