The Django admin site is not activated by default — it’s an opt-in thing. To activate the admin site for your installation, do these three things:
- Add "django.contrib.admin" to your INSTALLED_APPS setting.
- Run python manage.py syncdb. Since you have added a new application to INSTALLED_APPS, the database tables need to be updated.
- Edit your mysite/urls.py file and uncomment the line below “Uncomment this for admin:”. This file is a URLconf; we’ll dig into URLconfs in the next tutorial. For now, all you need to know is that it maps URL roots to applications.
class Poll(models.Model):
# ...
class Admin:
pass
class Admin:fields = (
(None, {'fields': ('question',)}),
('Date information', {'fields': ('pub_date',), 'classes': 'collapse'}),
)
poll = models.ForeignKey(Poll, edit_inline=models.STACKED, num_in_admin=3)
choice = models.CharField(maxlength=200, core=True)
votes = models.IntegerField(core=True)
poll = models.ForeignKey(Poll, edit_inline=models.TABULAR, num_in_admin=3)
Customize the admin change list
class Poll(models.Model):
# ...
class Admin:
# ...
list_display = ('question', 'pub_date')
list_display = ('question', 'pub_date', 'was_published_today')
#was_published_today custom method
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
was_published_today.short_description = 'Published today?'
list_filter = ['pub_date']
search_fields = ['question']
date_hierarchy = 'pub_date'
detail see: http://www.djangoproject.com/documentation/model-api/
Customize the admin look and feel
Clearly, having “Django administration” and “example.com” at the top of each admin page is ridiculous. It’s just placeholder text.
That’s easy to change, though, using Django’s template system. The Django admin is powered by Django itself, and its interfaces use Django’s own template system. (How meta!)
Open your settings file (mysite/settings.py, remember) and look at the TEMPLATE_DIRS setting. TEMPLATE_DIRS is a tuple of filesystem directories to check when loading Django templates. It’s a search path.
By default, TEMPLATE_DIRS is empty. So, let’s add a line to it, to tell Django where our templates live:
TEMPLATE_DIRS = (
"/home/my_username/mytemplates", # Change this to your own directory.
)
Now copy the template admin/base_site.html from within the default Django admin template directory (django/contrib/admin/templates) into an admin subdirectory of whichever directory you’re using in TEMPLATE_DIRS. For example, if your TEMPLATE_DIRS includes "/home/my_username/mytemplates", as above, then copy django/contrib/admin/templates/admin/base_site.html to /home/my_username/mytemplates/admin/base_site.html. Don’t forget that admin subdirectory.
Then, just edit the file and replace the generic Django text with your own site’s name and URL as you see fit.
Note that any of Django’s default admin templates can be overridden. To override a template, just do the same thing you did with base_site.html — copy it from the default directory into your custom directory, and make changes.
Astute readers will ask: But if TEMPLATE_DIRS was empty by default, how was Django finding the default admin templates? The answer is that, by default, Django automatically looks for a templates/ subdirectory within each app package, for use as a fallback. See the loader types documentation for full information.
Customize the admin index page
On a similar note, you might want to customize the look and feel of the Django admin index page.
By default, it displays all available apps, according to your INSTALLED_APPS setting. But the order in which it displays things is random, and you may want to make significant changes to the layout. After all, the index is probably the most important page of the admin, and it should be easy to use.
The template to customize is admin/index.html. (Do the same as with admin/base_site.html in the previous section — copy it from the default directory to your custom template directory.) Edit the file, and you’ll see it uses a template tag called {% get_admin_app_list as app_list %}. That’s the magic that retrieves every installed Django app. Instead of using that, you can hard-code links to object-specific admin pages in whatever way you think is best.
Django offers another shortcut in this department. Run the command python manage.py adminindex polls to get a chunk of template code for inclusion in the admin index template. It’s a useful starting point.
For full details on customizing the look and feel of the Django admin site in general, see the Django admin CSS guide.
No comments:
Post a Comment