The Django Book

Chapter 6: The Django Administration Site

绗叚绔狅細Django绠$悊绔欑偣

For a certain class of Web sites, an admin interface is an essential part of the infrastructure. This is a Web-based interface, limited to trusted site administrators, that enables the adding, editing and deletion of site content. The interface you use to post to your blog, the backend site managers use to moderate reader-generated comments, the tool your clients use to update the press releases on the Web site you built for them these are all examples of admin interfaces.

443w04 <a href=”http://fefjedvognoi.com/“>fefjedvognoi</a>, [url=http://krjjawlaijfr.com/]krjjawlaijfr[/url], [link=http://atdhxrjyjdqa.com/]atdhxrjyjdqa[/link], http://ozvqjgfxzzuk.com/

Theres a problem with admin interfaces, though: its boring to build them. Web development is fun when youre developing public-facing functionality, but building admin interfaces is always the same. You have to authenticate users, display and handle forms, validate input, and so on. Its boring, and its repetitive.

浣嗘槸绠$悊鐣岄潰鏈変竴闂锛氬垱寤哄畠澶箒鐞愩傚綋浣犲紑鍙戝鍏紬鐨勫姛鑳芥椂锛岀綉椤靛紑鍙戞槸鏈夎叮鐨勶紝浣嗘槸鍒涘缓绠$悊鐣岄潰閫氬父鏄崈绡囦竴寰嬬殑銆備綘蹇呴』璁よ瘉鐢ㄦ埛锛屾樉绀哄苟绠$悊琛ㄦ牸锛岄獙璇佽緭鍏ョ殑鏈夋晥鎬ц濡傛绫汇傝繖寰堢箒鐞愯屼笖鏄噸澶嶅姵鍔ㄣ

So whats Djangos approach to these boring, repetitive tasks? It does it all for youin just a couple of lines of code, no less. With Django, building an admin interface is a solved problem.

AfDNib <a href=”http://rshwegklwave.com/“>rshwegklwave</a>, [url=http://gnwaxzkgvuae.com/]gnwaxzkgvuae[/url], [link=http://vqyinvjocprg.com/]vqyinvjocprg[/link], http://yfwmaytrhrce.com/

This chapter is about Djangos automatic admin interface. This feature works by reading metadata in your model to provide a powerful and production-ready interface that site administrators can start using immediately. Here, we discuss how to activate, use, and customize this feature.

杩欎竴绔犳槸鍏充簬 Django 鐨勮嚜鍔ㄧ鐞嗙晫闈€傝繖涓壒鎬ф槸杩欐牱璧蜂綔鐢ㄧ殑锛氬畠璇诲彇浣犳ā寮忎腑鐨勫厓鏁版嵁锛岀劧鍚庢彁渚涚粰浣犱竴涓己澶ц屼笖鍙互浣跨敤鐨勭晫闈紝缃戠珯绠$悊鑰呭彲浠ョ敤瀹冪珛鍗冲伐浣溿傚湪杩欓噷鎴戜滑灏嗚璁哄浣曟縺娲伙紝浣跨敤鍜屽畾鍒惰繖涓壒鎬с

Activating the Admin Interface

wXaEzU <a href=”http://weihhhcnehtp.com/“>weihhhcnehtp</a>, [url=http://cygyknhhbhgh.com/]cygyknhhbhgh[/url], [link=http://fmrqoxrrjjgi.com/]fmrqoxrrjjgi[/link], http://zjpazexukfjv.com/

We think the admin interface is the coolest part of Djangoand most Djangonauts agreebut since not everyone actually needs it, its an optional piece. That means there are three steps youll need to follow to activate it:

鎴戜滑璁や负绠$悊鐣岄潰鏄 Django 涓渶閰风殑涓閮ㄥ垎锛屽ぇ閮ㄥ垎 Django 鐢ㄦ埛涔熻繖涔堟兂銆備絾鏄笉鏄墍鏈変汉閮介渶瑕佸畠锛屾墍浠ュ畠鏄彲閫夌殑銆傝繖涔熷氨鎰忓懗鐫浣犻渶瑕佽窡鐫涓変釜姝ラ鏉ユ縺娲诲畠銆

Add admin metadata to your models.

鍦ㄤ綘鐨 models 涓姞鍏ョ鐞嗗厓鏁版嵁銆

Not all models can (or should) be editable by admin users, so you need to mark models that should have an admin interface. You do that by adding an inner Admin class to your model (alongside the Meta class, if you have one). So, to add an admin interface to our Book model from the previous chapter, we use this:

ssmceh <a href=”http://jssgdqehhkdq.com/“>jssgdqehhkdq</a>, [url=http://cutdvwkocnbr.com/]cutdvwkocnbr[/url], [link=http://lnqtxbzhqhdy.com/]lnqtxbzhqhdy[/link], http://exyywmeiopzz.com/

class Book(models.Model):
    title = models.CharField(maxlength=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()
    num_pages = models.IntegerField(blank=True, null=True)

    def __str__(self):
        return self.title

    **class Admin:**
        **pass**

The Admin declaration flags the class as having an admin interface. There are a number of options that you can put beneath Admin , but for now were sticking with all the defaults, so we put pass in there to signify to Python that the Admin class is empty.

Admin 澹版槑鏍囧織浜嗚绫绘湁涓涓鐞嗙晫闈€傚湪 Admin 涔嬩笅浣犲彲浠ユ斁寰堝閫夐」锛屼絾鐩墠鎴戜滑鍙叧娉ㄧ己鐪佺殑涓滆タ锛屾墍浠ユ垜浠彧鍦ㄩ偅鍐欎笂 pass 璁 Python 鐭ラ亾 Admin 绫绘槸绌虹殑銆

If youre following this example with your own code, its probably a good idea to add Admin declarations to the Publisher and Author classes at this point.

濡傛灉浣犳璺熺潃渚嬪瓙鍦ㄥ啓浣犵殑浠g爜锛岀幇鍦ㄤ綘鍙互鍦 PublisherAuthor 绫讳腑鍔犲叆 Admin 澹版槑銆

Install the admin application. Do this by adding "django.contrib.admin" to your INSTALLED_APPS setting.

瀹夎绠$悊搴旂敤绋嬪簭銆傚湪浣犵殑 INSTALLED_APPS 鐨勮缃腑鍔犲叆 "django.contrib.admin"

If youve been following along, make sure that "django.contrib.sessions" , "django.contrib.auth" , and "django.contrib.contenttypes" are uncommented, since the admin application depends on them. Also uncomment all the lines in the MIDDLEWARE_CLASSES setting tuple and delete the TEMPLATE_CONTEXT_PROCESSOR setting to allow it to take the default values again.

濡傛灉浣犳槸涓鐩寸収姝ラ鍋氫笅鏉ョ殑锛岃纭 "django.contrib.sessions" , "django.contrib.auth" , 鍜 "django.contrib.contenttypes" 鍓嶉潰鐨勬敞閲婂凡鍘绘帀锛屽洜涓虹鐞嗙▼搴忛渶瑕佸畠浠傝鍚屾椂鍘绘帀鎵鏈 MIDDLEWARE_CLASSES 璁剧疆琛屼腑鐨勬敞閲婏紝骞舵竻闄 TEMPLATE_CONTEXT_PROCESSOR 璁剧疆锛屼互渚垮畠鍙互閲嶆柊浣跨敤缂虹渷鍊笺

Run python manage.py syncdb . This step will install the extra database tables the admin interface uses.

杩愯 python manage.py syncdb 銆傝繖涓姝ュ皢鐢熸垚绠$悊鐣岄潰浣跨敤鐨勯澶栨暟鎹簱琛ㄣ

Note

娉ㄩ噴

When you first run syncdb with "django.contrib.auth" in INSTALLED_APPS, youll be asked about creating a superuser. If you didnt do so at that time, youll need to run django/contrib/auth/bin/create_superuser.py to create an admin user. Otherwise, you wont be able to log in to the admin interface.

EzpmtF <a href=”http://rhzdahxlffgj.com/“>rhzdahxlffgj</a>, [url=http://inqjkqhlhfwx.com/]inqjkqhlhfwx[/url], [link=http://znkymulybhxz.com/]znkymulybhxz[/link], http://dfptccfdqpcd.com/

Add the URL pattern to your urls.py . If youre still using the one created by startproject , the admin URL pattern should be already there, but commented out. Either way, your URL patterns should look like the following:

鍦ㄤ綘鐨 urls.py 涓姞鍏ユā鏉裤傚鏋滀綘浠嶅湪鐢 startproject 鐢熸垚鐨 urls.py 鏂囦欢锛岀鐞 URL 妯℃澘宸茬粡鍦ㄩ噷闈簡锛屼綘闇瑕佸幓鎺夋敞閲娿備换浣曚竴涓柟寮忕殑 URL 妯℃澘搴旇鍍忎笅闈㈣繖鏍凤細

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    **(r'^admin/', include('django.contrib.admin.urls')),**
)

Thats it. Now run python manage.py runserver to start the development server. Youll see something like this:

灏辨槸杩欐牱銆傜幇鍦ㄨ繍琛 python manage.py runserver 浠ュ惎鍔ㄥ紑鍙戞湇鍔″櫒銆備綘灏嗙湅鍒板儚涓嬮潰杩欐牱鐨勪笢瑗匡細

Validating models...
0 errors found.

Django version 0.96, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now you can visit the URL given to you by Django (http://127.0.0.1:8000/admin/ in the preceding example), log in, and play around.

75iR1p <a href=”http://zjfiizgkoffk.com/“>zjfiizgkoffk</a>, [url=http://uymppgtvzoii.com/]uymppgtvzoii[/url], [link=http://hfsaokslvzyd.com/]hfsaokslvzyd[/link], http://clapjliqvoha.com/

Using the Admin Interface

bHK7x0 <a href=”http://wokhzphanabe.com/“>wokhzphanabe</a>, [url=http://gbjizyicjwpp.com/]gbjizyicjwpp[/url], [link=http://hhzxzdgcdypv.com/]hhzxzdgcdypv[/link], http://rkvbqoousbyl.com/

The admin interface is designed to be used by nontechnical users, and as such it should be pretty self-explanatory. Nevertheless, a few notes about the features of the admin interface are in order.

绠$悊鐣岄潰鐨勮璁℃槸閽堝闈炴妧鏈汉鍛樼殑锛屾墍浠ュ畠搴旇鏄嚜鎴戣В閲婄殑銆傛棤璁哄浣曪紝鏈夊叧绠$悊鐣岄潰鐗规х殑涓浜涙敞閲婃槸瀹屽杽鐨勩

The first thing youll see is a login screen, as shown in Figure 6-1.

浣犵湅鍒扮殑绗竴浠朵簨鏄鍥6-1鎵绀虹殑鐧诲綍灞忓箷銆

Screenshot of Djangos login page. Django 鐧诲綍椤甸潰鐨勬埅鍥俱

Figure 6-1. Djangos login screen

鍥 6-1.Django 鐧诲綍灞忓箷

Youll use the username and password you set up when you added your superuser. Once youre logged in, youll see that you can manage users, groups, and permissions (more on that shortly).

23LNh6 <a href=”http://mzvfoagylqtm.com/“>mzvfoagylqtm</a>, [url=http://icmjlnilarub.com/]icmjlnilarub[/url], [link=http://hlthrugzziap.com/]hlthrugzziap[/link], http://zhbtqnsswfjy.com/

Each object given an Admin declaration shows up on the main index page, as shown in Figure 6-2.

姣忎竴涓湁 Admin 澹版槑鐨勫璞¢兘鍦ㄤ富绱㈠紩椤垫樉绀猴紝瑙佸浘 6-2銆

Screenshot of the main Django admin index. Django 涓荤鐞嗙储寮曟埅鍥俱

Figure 6-2. The main Django admin index

鍥 6-2銆侱jango 涓荤鐞嗙储寮

Links to add and change objects lead to two pages we refer to as object change lists and edit forms . Change lists are essentially index pages of objects in the system, as shown in Figure 6-3.

娣诲姞鍜屾洿鏀瑰鍍忕殑閾炬帴灏嗗鍑轰袱涓〉闈紝杩欎袱涓〉闈㈡槸鎸囧悜 鏇存敼鍒楄〃缂栬緫琛ㄦ牸 涓や釜瀵瑰儚銆傚鍥6-3鎵绀猴紝鏇存敼鍒楄〃涓昏鏄郴缁熷鍍忕殑绱㈠紩椤甸潰銆

Screenshot of a typical change list view.

UZuii2 <a href=”http://plibwuonusdz.com/“>plibwuonusdz</a>, [url=http://zavtmjclcxbb.com/]zavtmjclcxbb[/url], [link=http://aypkcwjxfsiq.com/]aypkcwjxfsiq[/link], http://hopgkkaubpci.com/

Figure 6-3. A typical change list view

鍥 6-3. 鍏稿瀷鐨勬敼鍙樺垪琛ㄨ鍥

A number of options control which fields appear on these lists and the appearance of extra features like date drill-downs, search fields, and filter interfaces. We discuss these features in more detail shortly.

鍦ㄨ繖浜涘垪琛ㄩ噷鐨勬爮鐩湁涓嶅皯閫夐」鎺у埗锛岃繖浜涙樉绀哄嚭涓浜涢澶栫殑鐗规э紝姣斿涓嬫媺寮忔棩鏈熼夋嫨鎺у埗锛屾悳绱㈡爮锛岃繃婊ょ晫闈€傛垜浠◢鍚庤璁鸿繖浜涚壒鎬х殑缁嗚妭銆

Edit forms are used to modify existing objects and create new ones (see Figure 6-4). Each field defined in your model appears here, and youll notice that fields of different types get different widgets (e.g., date/time fields have calendar controls, foreign keys use a select box, etc.).

缂栬緫琛ㄦ牸鏄敤鏉ヤ慨鏀圭幇鏈夊璞″拰鍒涘缓鏂板璞$殑锛堣鍥6-4锛夈傚湪浣犳ā寮忎腑瀹氫箟鐨勫煙鍦ㄨ繖閲岄兘鏄剧ず鍑烘潵锛屼綘涔熻娉ㄦ剰鍒颁笉鍚岀被鍨嬬殑鍩熺敤涓嶅悓鐨勬帶浠舵樉绀猴紙濡傦細鏃ユ湡/鏃堕棿鍩熸湁鏃ュ巻鎺т欢锛屽閿敤閫夋嫨鏍忕瓑绛夛級銆

Screenshot of a typical edit form. 鍏稿瀷鐨勭紪杈戣〃鏍兼埅鍥俱

Figure 6-4. A typical edit form

鍥 6-4. 鍏稿瀷鐨勭紪杈戣〃鏍

Youll notice that the admin interface also handles input validation for you. Try leaving a required field blank or putting an invalid time into a time field, and youll see those errors when you try to save, as shown in Figure 6-5.

浣犺繕鑳界湅鍒扮鐞嗙晫闈篃鎺у埗鐫浣犺緭鍏ョ殑鏈夋晥鎬с備綘鍙互璇曡瘯涓嶅~蹇呴渶鐨勬爮鐩垨鑰呭湪鏃堕棿鏍忛噷濉敊璇殑鏃堕棿锛屼綘浼氬彂鐜板綋浣犺淇濆瓨鏃朵細鍑虹幇閿欒淇℃伅锛屽鍥6-5鎵绀恒

Screenshot of an edit form displaying errors. 缂栬緫琛ㄦ牸鏄剧ず閿欒淇℃伅鐨勬埅鍥俱

Figure 6-5. An edit form displaying errors

鍥6-5. 缂栬緫琛ㄦ牸鏄剧ず閿欒淇℃伅

When you edit an existing object, youll notice a History button in the upper-right corner of the window. Every change made through the admin interface is logged, and you can examine this log by clicking the History button (see Figure 6-6).

褰撲綘缂栬緫宸叉湁鐨勫鍍忔椂锛屼綘鍦ㄧ獥鍙g殑鍙充笂瑙掑彲浠ョ湅鍒颁竴涓巻鍙叉寜閽傞氳繃绠$悊鐣岄潰鍋氱殑姣忎竴涓敼鍙橀兘鐣欐湁璁板綍锛屼綘鍙互鎸夊巻鍙查敭鏉ユ鏌ヨ繖涓褰曪紙瑙佸浘6-6锛夈

Screenshot of Djangos object history page. Django 鍘嗗彶椤甸潰鎴浘銆

Figure 6-6. Djangos object history page

鍥6-6. Django 瀵瑰儚鍘嗗彶椤甸潰

When you delete an existing object, the admin interface asks you to confirm the delete action to avoid costly mistakes. Deletions also cascade ; the deletion confirmation page shows you all the related objects that will be deleted as well (see Figure 6-7).

褰撲綘鍒犻櫎鐜版湁瀵硅薄鏃讹紝绠$悊鐣岄潰浼氳姹備綘纭鍒犻櫎鍔ㄤ綔浠ュ厤寮曡捣涓ラ噸閿欒銆傚垹闄や篃鏄*鑱旂骇*鐨勶細鍒犻櫎纭椤甸潰浼氭樉绀烘墍鏈夊皢瑕佸垹闄ょ殑鍏宠仈瀵硅薄锛堣鍥6-7锛夈

Screenshot of Djangos delete confirmation page. Django 鍒犻櫎纭椤甸潰鎴浘銆

Figure 6-7. Djangos delete confirmation page

鍥 6-7. Django 鍒犻櫎纭椤甸潰

Users, Groups, and Permissions

鐢ㄦ埛銆佺粍鍜岃鍙

Since youre logged in as a superuser, you have access to create, edit, and delete any object. However, the admin interface has a user permissions system that you can use to give other users access only to the portions of the interface that they need.

鍥犱负浣犳槸鐢ㄨ秴绾х敤鎴风櫥褰曠殑锛屼綘鍙互鍒涘缓锛岀紪杈戝拰鍒犻櫎浠讳綍瀵瑰儚銆傜劧鑰岀鐞嗙晫闈㈡湁涓涓敤鎴疯鍙郴缁燂紝浣犲彲浠ョ敤瀹冩潵缁欏叾瀹冪敤鎴锋巿涓庝粬浠渶瑕佺殑閮ㄥ垎鏉冨姏銆

You edit these users and permissions through the admin interface just like any other object. The link to the User and Group models is there on the admin index along with all the objects youve defined yourself.

浣犻氳繃绠$悊鐣岄潰缂栬緫鐢ㄦ埛鍙婂叾璁稿彲灏卞儚浣犵紪杈戝埆鐨勫璞′竴鏍枫 鐢ㄦ埛 妯″紡鐨勯摼鎺ュ拰浣犺嚜宸卞畾涔夌殑鎵鏈夊鍍忎竴鏍峰垪鍦ㄧ鐞嗙储寮曢〉闈€

User objects have the standard username, password, e-mail, and real name fields you might expect, along with a set of fields that define what the user is allowed to do in the admin interface. First, theres a set of three flags:

鐢ㄦ埛瀵瑰儚鏈変綘鏈熸湜鐨勬爣鍑嗙敤鎴峰悕锛屽瘑鐮侊紝e-mail鍜岀湡瀹炲鍚嶅煙锛屽悓鏃跺畠杩樻湁鍦ㄧ鐞嗙晫闈㈤噷杩欎釜鐢ㄦ埛鍙互鍋氫粈涔堛傞鍏堬紝杩欐湁涓缁勪笁涓爣璁帮細

  • The is active flag controls whether the user is active at all. If this flag is off, the user has no access to any URLs that require login.

  • 杩欐槸婵娲绘爣蹇楋紝瀹冪敤鏉ユ帶鍒剁敤鎴锋槸鍚﹀凡缁忔縺娲汇傚鏋滆繖涓爣蹇楀叧闂紝杩欎釜鐢ㄦ埛灏变笉鑳芥祻瑙堜换浣曢渶瑕佺櫥褰曠殑URL銆

  • The is staff flag controls whether the user is allowed to log in to the admin interface (i.e., whether that user is considered a staff member in your organization). Since this same user system can be used to control access to public (i.e., non-admin) sites (see Chapter 12), this flag differentiates between public users and administrators.

jDypZh <a href=”http://rgvvorujyatc.com/“>rgvvorujyatc</a>, [url=http://tncyagsvklya.com/]tncyagsvklya[/url], [link=http://juugtzroyyqb.com/]juugtzroyyqb[/link], http://mrzcsbglrvog.com/

  • The is superuser flag gives the user full, unfettered access to every item in the admin interface; regular permissions are ignored.

  • 杩欐槸瓒呯骇鐢ㄦ埛鏍囧織锛屽畠缁欑敤鎴锋墍鏈夋潈闄愶紝鍦ㄧ鐞嗙晫闈㈠彲浠ヨ嚜鐢辫繘鍏ワ紝甯歌璁稿彲鏃犳晥銆

Normal admin usersthat is, active, non-superuser staff membersare granted access that depends on a set of assigned permissions. Each object editable through the admin interface has three permissions: a create permission, an edit permission, and a delete permission. Assigning permissions to a user grants the user access to do what is described by those permissions.

鏅氱殑娲昏穬锛岄潪瓒呯骇鐢ㄦ埛鐨勭鐞嗙敤鎴峰彲浠ユ牴鎹竴濂楄瀹氬ソ鐨勮鍙繘鍏ャ傞氳繃绠$悊鐣岄潰缂栬緫鐨勬瘡涓鍍忔湁涓変釜璁稿彲锛 鍒涘缓 璁稿彲锛 缂栬緫 璁稿彲鍜 鍒犻櫎 璁稿彲銆傜粰涓涓敤鎴锋巿鏉冭鍙篃灏辫〃鏄庤鐢ㄦ埛鍙互杩涜璁稿彲鎻忚堪鐨勬搷浣溿

Note

Access to edit users and permissions is also controlled by this permission system. If you give someone permission to edit users, she will be able to edit her own permissions, which might not be what you want!

鏉冮檺绠$悊绯荤粺涔熸帶鍒剁紪杈戠敤鎴峰拰鏉冮檺銆傚鏋滀綘缁欐煇浜虹紪杈戠敤鎴风殑鏉冮檺锛屼粬鍙互缂栬緫鑷繁鐨勬潈闄愶紝杩欑鑳藉姏鍙兘涓嶆槸浣犲笇鏈涚殑銆

You can also assign users to groups. A group is simply a set of permissions to apply to all members of that group. Groups are useful for granting identical permissions to large number of users.

浣犱篃鍙互缁欑粍涓垎閰嶇敤鎴枫備竴涓 绠鍖栦簡缁欑粍涓墍鏈夋垚鍛樺簲鐢ㄤ竴濂楄鍙殑鍔ㄤ綔銆 缁勫湪缁欏ぇ閲忕敤鎴风壒瀹氭潈闄愮殑鏃跺欏緢鏈夌敤銆

Customizing the Admin Interface

瀹氬埗绠$悊鐣岄潰

You can customize the way the admin interface looks and behaves in a number of ways. We cover just a few of them in this section as they relate to our Book model; Chapter 17 covers customizing the admin interface in detail.

浣犲彲浠ラ氳繃寰堝鏂规硶鏉ュ畾鍒剁鐞嗙晫闈㈢殑澶栬鍜岃涓恒傚湪鏈妭鎴戜滑鍙皥鍙婁笌鎴戜滑 Book 鐩稿叧鐨 涓浜涙柟娉曪紝绗崄涓冪珷灏嗚璁哄畾鍒剁鐞嗙晫闈㈢殑缁嗚妭闂銆

As it stands now, the change list for our books shows only the string representation of the model we added to its __str__ . This works fine for just a few books, but if we had hundreds or thousands of books, it would be very hard to locate a single needle in the haystack. However, we can easily add some display, searching, and filtering functions to this interface. Change the Admin declaration as follows:

鐩墠涓烘鎴戜滑涔︾殑鏀瑰彉鍒楄〃鍙樉绀轰竴涓瓧绗︿覆锛岃繖涓瓧绗︿覆鏄湪妯″紡涓殑 __str__ 涓姞鍏ユ潵浠h〃杩欎釜妯″紡鐨勩傝繖绉嶆柟寮忓湪鍙湁鍑犳湰涔︾殑鎯呭喌涓嬪伐浣滃緱寰堝ソ锛屼絾濡傛灉鏈夋垚鐧句笂鍗冧腑涔︾殑鏃跺欙紝鎵句竴鏈功灏卞儚澶ф捣鎹為拡銆備絾鏄垜浠彲浠ュ緢瀹规槗鍦板湪鐣岄潰涓姞鍏ユ悳绱㈠拰杩囨护鍔熻兘銆傛敼鍙 Admin 澹版槑濡備笅锛

class Book(models.Model):
    title = models.CharField(maxlength=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    class Admin:
        **list_display = ('title', 'publisher', 'publication_date')**
        **list_filter = ('publisher', 'publication_date')**
        **ordering = ('-publication_date',)**
        **search_fields = ('title',)**

These four lines of code dramatically change our list interface, as shown in Figure 6-8.

杩欏洓琛屼唬鐮佹垙鍓фу湴鏀瑰彉浜嗘垜浠殑鍒楄〃鐣岄潰锛屽鍥6-8鎵绀恒

Screenshot of the modified change list page.

WAV4Bz <a href=”http://xffcjccqrjdc.com/“>xffcjccqrjdc</a>, [url=http://xnhmwkaiqsrk.com/]xnhmwkaiqsrk[/url], [link=http://wvubidzachtc.com/]wvubidzachtc[/link], http://cebhekomljne.com/

Figure 6-8. Modified change list page

鍥 6-8. 淇敼杩囩殑鍙樺寲鍒楄〃椤甸潰

Each of those lines instructed the admin interface to construct a different piece of this interface:

姣忎竴琛岃鏄庣鐞嗙晫闈㈢殑涓嶅悓閮ㄥ垎锛

The list_display option controls which columns appear in the change list table. By default, the change list displays only a single column that contains the objects string representation. Here, weve changed that to show the title, publisher, and publication date.

list_display 閫夐」鎺у埗鍙樻洿鍒楄〃鎵鏄剧ず鐨勫垪銆傜己鐪佹儏鍐典笅鍙樻洿鍒楄〃鍙樉绀哄鍍忓寘鍚殑 琛ㄥ緛瀛楃涓层傛垜浠湪杩欐敼鍙樻垚鏄剧ず鏍囬锛屽嚭鐗堝晢鍜屽嚭鐗堟棩鏈熴

The list_filter option creates the filtering bar on the right side of the list. Weve allowed filtering by date (which allows you to see only books published in the last week, month, etc.) and by publisher.

list_filter 閫夐」鍦ㄥ彸杈瑰垱寤轰竴涓繃婊ゆ潯銆傛垜浠厑璁稿畠鎸夋棩鏈熻繃婊わ紙瀹冨彲浠ヨ浣犲彧鏄剧ず杩囧幓涓鍛紝涓涓湀绛夌瓑鍑虹増鐨勪功绫嶏級鍜屾寜鍑虹増鍟嗚繃婊ゃ

You can instruct the admin interface to filter by any field, but foreign keys, dates, Booleans, and fields with a choices attribute work best. The filters show up as long as there are at least 2 values to choose from.

浣犲彲浠ュ湪绠$悊鐣岄潰涓寚瀹氫换浣曞煙鍋氫负杩囨护鍣紝浣嗘槸鐢ㄥ閿紝鏃ユ湡锛屽竷灏斿煎拰鏈 choices 灞炴х殑鍩熸槸鏈閫傚悎鐨勩傝繃婊よ嚦灏戞樉绀2涓笺

The ordering option controls the order in which the objects are presented in the admin interface. Its simply a list of fields by which to order the results; prefixing a field with a minus sign reverses the given order. In this example, were ordering by publication date, with the most recent first.

ordering 閫夐」鎺у埗瀵硅薄鍦ㄧ鐞嗙晫闈㈡樉绀烘椂鐨勬帓搴忔柟寮忋傚畠鏄兂瑕佹寜搴忔帓鍒楃殑瀛楁鐨 鍒楄〃锛涘墠闈㈠甫鍑忓彿锛-锛夌殑鎸夐嗗簭鎺掑簭銆傚湪杩欎釜渚嬪瓙涓紝鎴戜滑鎸塸ublication date鎺掑簭锛 鏈杩戠殑鎺掑湪鏈鍓嶃

Finally, the search_fields option creates a field that allows text searches. It allows searches by the title field (so you could type Django to show all books with Django in the title).

鏈鍚庯紝 search_fields 閫夐」鍒涘缓浜嗕竴涓厑璁告悳绱㈡枃鏈唴瀹圭殑鍩熴傚畠鍙互鎼滅储 title 瀛楁涓殑鍐呭锛堟墍浠ユ偍鍙互杈撳叆 Django 浠ユ樉绀烘墍鏈夐鍚嶄腑鍖呭惈鏈 Django 鐨勪功绫嶏級銆

Using these options (and the others described in Chapter 12) you can, with only a few lines of code, make a very powerful, production-ready interface for data editing.

閫氳繃浣跨敤杩欎簺閫夐」锛堣繕鏈変竴浜涙槸鍦ㄥ崄浜岀珷鎻忚堪鐨勶級浣犺兘澶熺敤寰堝皯鐨勪唬鐮佸疄鐜板緢寮哄ぇ锛屼骇鍝佺骇鐨勬暟鎹紪杈戠晫闈€

Customizing the Admin Interfaces Look and Feel

瀹氬埗绠$悊鐣岄潰鐨勫瑙傚拰鎰熻

Clearly, having the phrase Django administration at the top of each admin page is ridiculous. Its just placeholder text.

鏄剧劧锛屽鏋滃湪姣忎釜绠$悊椤甸潰鐨勫ご閮ㄩ兘鍖呭惈鈥淒jango administration鈥濊繖琛屽瓧鏄悶绗戠殑銆傝繖琛屽瓧鍙槸鍧楁爣绛剧殑鍗犱綅绗︺

Its easy to change, though, using Djangos template system. The Django admin site is powered by Django itself, and its interfaces use Djangos own template system. (Djangos template system was covered in Chapter 4.)

閫氳繃Django妯℃澘绯荤粺鍙互寰堝鏄撶殑淇敼瀹冦侱jango绠$悊绔欑偣鍚屾牱鏄敤Django缂栧啓鐨勶紝瀹冪殑鐢ㄦ埛 鐣岄潰浣跨敤Django鑷繁鐨勬ā鏉跨郴缁熴傦紙鍏充簬Django妯℃澘绯荤粺璇峰弬瑙佺鍥涚珷銆傦級

As we explained in Chapter 4, the TEMPLATE_DIRS setting specifies a list of directories to check when loading Django templates. To customize Djangos admin templates, simply copy the relevant stock admin template from the Django distribution into your one of the directories pointed-to by TEMPLATE_DIRS .

鎴戜滑鍦ㄧ鍥涚珷宸茬粡璁插埌锛 TEMPLATE_DIRS 閰嶇疆璁剧疆浜咲jango鍔犺浇妯℃澘鐨勭洰褰曞垪琛ㄣ 瑕佽嚜瀹氫箟Django鐨勭鐞嗘ā鏉匡紝鍙渶瑕佹嫹璐滵jango鍙戣鐗堜腑鐨勬暣涓鐞嗘ā鏉垮埌浣犲湪 TEMPLATE_DIRS 閲岃缃殑妯℃澘鐩綍閲屻

The admin site finds the Django administration header by looking for the template admin/base_site.html . By default, this template lives in the Django admin template directory, django/contrib/admin/templates , which you can find by looking in your Python site-packages directory, or wherever Django was installed. To customize this base_site.html template, copy that template into an admin subdirectory of whichever directory youre using in TEMPLATE_DIRS . For example, if your TEMPLATE_DIRS includes "/home/mytemplates" , then copy django/contrib/admin/templates/admin/base_site.html to /home/mytemplates/admin/base_site.html . Dont forget that admin subdirectory.

绠$悊绔欑偣鐨勫ご閮ㄥ尯鍩熷湪妯℃澘 admin/base_site.html 閲屻傜己鐪佹儏鍐典笅锛岃繖涓ā鏉垮湪 Django绠$悊妯℃澘鐩綍 django/contrib/admin/templates 閲岋紝浣犲彲浠ュ湪Django鐨勫畨瑁 鐩綍鎵惧埌瀹冿紝渚嬪Python鐨 site-packages 鐩綍鎴栬呬綘瀹夎鐨勫叾浠栫洰褰曘傝鑷畾涔 杩欎釜 base_site 妯℃澘锛屾妸杩欎釜妯℃澘鎷疯礉鍒颁綘鐨勬ā鏉跨洰褰曚笅鐨 admin 瀛愮洰褰曘 渚嬪锛屽亣瀹氫綘鐨勬ā鏉跨洰褰曟槸 "/home/mytemplates" 锛屾嫹璐 django/contrib/admin/templates/admin/base_site.html/home/mytemplates/admin/base_site.html 銆備笉瑕佸繕浜嗘湁 admin 瀛愮洰褰曘

Then, just edit the new admin/base_site.html file to replace the generic Django text with your own sites name as you see fit.

鐒跺悗锛岀紪杈戣繖涓柊 admin/base_site.html 鏂囦欢锛屾浛鎹綘鑷繁绔欑偣鐨勫悕绉颁笂鍘汇

Note that any of Djangos 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 to the copy.

澶囨敞 姣忎釜Django缂虹渷鐨勭鐞嗘ā鏉块兘鍙互閲嶈浇銆傝閲嶈浇涓涓ā鏉匡紝灏辫薄 base_site.html 涓鏍风殑鍘诲仛锛氭妸瀹冧粠缂虹渷鐩綍涓嫹璐濆埌浣犺嚜宸辩殑妯℃澘鐩綍涓劧鍚庝慨鏀瑰畠銆

You might wonder how, if TEMPLATE_DIRS was empty by default, Django found the default admin templates. The answer is that, by default, Django automatically looks for templates within a templates/ subdirectory in each application package as a fallback. See the Writing Custom Template Loaders in Chapter 10 for more information about how this works.

浣犲彲鑳戒細鎯冲埌鏄繖涔堜竴鍥炰簨锛屽鏋 TEMPLATE_DIRS 缂虹渷鏄┖鐨勶紝Django灏变娇鐢ㄧ己鐪佺殑绠$悊 妯℃澘銆傛纭殑鍥炵瓟鏄紝缂虹渷鎯呭喌涓嬶紝Django鑷姩鍦ㄦ瘡涓猘pp閲岀殑 templates/ 瀛愮洰褰曢噷鎼滅储 妯℃澘鏉ュ仛鍚庡銆傚叿浣撹鐪嬬鍗佺珷涓殑缂栧啓鑷畾涔夋ā鏉垮姞杞藉櫒绔犺妭銆

Customizing 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 applications, according to your INSTALLED_APPS setting, sorted by the name of the application. You might, however, want to change this order to make it easier to find the applications youre looking for. After all, the index is probably the most important page of the admin interface, so it should be easy to use.

浣犲悓鏍峰彲浠ヨ嚜瀹氫箟Django绠$悊鐨勭储寮曢〉闈紙index page)銆傜己鐪佹儏鍐典笅锛屽畠灏嗘樉绀哄湪 INSTALL_APPS 閰嶇疆閲岃缃殑鎵鏈夊簲鐢ㄧ▼搴忥紝鎸夊簲鐢ㄧ▼搴忕殑鍚嶇О鎺掑簭銆備綘鍙兘鎯宠淇敼 鎺掑簭鏂瑰紡鏉ヨ浣犳洿瀹规槗鎵惧埌浣犳兂瑕佺殑搴旂敤绋嬪簭銆傛瘯绔燂紝绱㈠紩鍙兘鏄鐞嗙晫闈腑鏈閲嶈鐨勯〉闈紝 鎵浠ヨ瀹规槗浣跨敤鎵嶈銆

The template to customize is admin/index.html . (Remember to copy admin/index.html to your custom template directory as in the previous example.) Edit the file, and youll see it uses a template tag called {% get_admin_app_list as app_list %} . This tag retrieves every installed Django application. Instead of using the tag, you can hard-code links to object-specific admin pages in whatever way you think is best. If hard-coding links doesnt appeal to you, see Chapter 10 for details on implementing your own template tags.

瑕佽嚜瀹氫箟鐨勬ā鏉挎槸 admin/index.html 銆傦紙璁板緱璞″墠闈緥瀛愪竴鏍锋嫹璐 admin/index.html 鍒颁綘鐨勬ā鏉跨洰褰曘傦級鎵撳紑杩欎釜鏂囦欢锛屼綘浼氱湅鍒颁竴涓彨鍋 {% get_admin_app_list as app_list %} 鐨勬ā鏉挎爣绛撅紝浣犲彲浠ュ湪杩欓噷纭紪鐮佷綘鎯宠鐨勭鐞嗛〉闈㈣繛鎺ャ傚鏋滀綘涓嶅枩娆㈢‖缂栫爜鐨勬柟寮忥紝璇峰弬鐪 绗崄绔犱腑瀹炵幇浣犺嚜宸辩殑妯℃澘鏍囩绔犺妭銆

Django offers another shortcut in this department. Run the command python manage.py adminindex <app> to get a chunk of template code for inclusion in the admin index template. Its a useful starting point.

鍦ㄨ繖閲岋紝Django鎻愪緵浜嗕竴涓揩鎹锋柟寮忋傝繍琛屽懡浠 python manage.py adminindex <app> 鏉ヨ幏鍙栧彲浠ュ寘鍚湪绠$悊绱㈠紩妯℃澘閲岀殑涓娈典唬鐮併傝繖鏄竴涓緢鏈夌敤鐨勮捣鐐广

For full details on customizing the look and feel of the Django admin site in general, see Chapter 17.

鏈夊叧浜嶥jango绠$悊绔欑偣鑷畾涔夌殑璇︾粏鍐呭锛岃鍙傜湅绗崄涓冪珷銆

When and Why to Use the Admin Interface

sg3FEJ <a href=”http://ywejhwkxcmnr.com/“>ywejhwkxcmnr</a>, [url=http://ihnmhhgbxycf.com/]ihnmhhgbxycf[/url], [link=http://mavsswnqkzlz.com/]mavsswnqkzlz[/link], http://qlsysxiywuco.com/

We think Djangos admin interface is pretty spectacular. In fact, wed call it one of Djangos killer features. However, we often get asked about use cases for the admin interfacewhen do we use it, and why? Over the years, weve discovered a number of patterns for using the admin interface that we think might be helpful.

鎴戜滑璁や负Django鐨勭鐞嗙晫闈㈡槸寰堟湁鍚稿紩鍔涚殑銆備簨瀹炰笂锛屾垜浠О瀹冧负Django鐨勬潃鎵嬮攺涔嬩竴銆傚綋鐒讹紝 鎴戜滑涔熺粡甯歌闂亾 鎴戜滑 搴旇鍦ㄤ粈涔堟儏鍐典笅浣跨敤绠$悊鐣岄潰锛屼负浠涔堝憿锛熷骞村疄璺电粡楠岃鎴戜滑 鍙戠幇浜嗕竴浜涗娇鐢ㄧ鐞嗙晫闈㈢殑妯″紡锛屼細瀵瑰ぇ瀹跺緢鏈夊府鍔┿

Obviously, the admin interface is extremely useful for editing data (fancy that). If you have any sort of data entry tasks, the admin interface simply cant be beat. We suspect that the vast majority of readers of this book will have a whole host of data entry tasks.

鏄剧劧锛孌jango绠$悊鐣岄潰瀵圭紪杈戞暟鎹壒鍒湁鐢紙闅句互缃俊鐨勬锛侊級銆傚鏋滀綘鏈変换浣曢渶瑕佽緭鍏ユ暟鎹殑 浠诲姟锛岀鐞嗙晫闈㈡槸鍐嶅悎閫備笉杩囦簡銆傛垜鐩镐俊澶у鑲畾閮芥湁寰堝瑕佽緭鍏ユ暟鎹殑浠诲姟鍚э紵

Djangos admin interface especially shines when nontechnical users need to be able to enter data; thats the purpose behind the feature, after all. At the newspaper where Django was first developed, development of a typical online featurea special report on water quality in the municipal supply, saygoes something like this:

Django鐨勭鐞嗙晫闈㈠闈炴妧鏈敤鎴疯杈撳叆浠栦滑鐨勬暟鎹椂鐗瑰埆鏈夌敤锛涗簨瀹炰笂杩欎釜鐗规у氨鏄笓闂ㄤ负杩欎釜 瀹炵幇鐨勩傚湪Django鏈寮濮嬪紑鍙戠殑鏂伴椈鎶ラ亾鐨勮涓氬簲鐢ㄤ腑锛屾湁涓涓吀鍨嬬殑鍦ㄧ嚎鑷潵姘寸殑姘磋川涓撻鎶ラ亾 搴旂敤锛屽畠鐨勫疄鐜版祦绋嬫槸杩欐牱鐨勶細

  • The reporter responsible for the story meets with one of the developers and goes over the available data.

  • 璐熻矗杩欎釜鎶ラ亾鐨勮鑰呭拰瑕佸鐞嗘暟鎹殑寮鍙戣呯澶达紝鎻愪緵涓浜涙暟鎹粰寮鍙戣呫

  • The developer designs a model around this data and then opens up the admin interface to the reporter.

  • 寮鍙戣呭洿缁曡繖浜涙暟鎹璁℃ā鍨嬬劧鍚庨厤缃竴涓鐞嗙晫闈㈢粰璁拌呫

  • While the reporter enters data into Django, the programmer can focus on developing the publicly accessible interface (the fun part!).

  • 鍦ㄨ鑰呰緭鍏ユ暟鎹埌Django涓幓鐨勬椂鍊欙紝缂栫▼浜哄憳灏卞彲浠ラ泦涓敞鎰忓姏鍒板紑鍙戝叕鍏辫闂晫闈笂 锛堣繖鍙槸鏈夎叮鐨勯儴鍒嗗晩锛侊級銆

In other words, the raison dtre of Djangos admin interface is facilitating the simultaneous work of content producers and programmers.

鎹㈠彞璇濊锛孌jango鐨勭鐞嗙晫闈负鍐呭杈撳叆浜哄憳鍜岀紪绋嬩汉鍛橀兘鎻愪緵浜嗕究鍒╃殑宸ュ叿銆

However, beyond the obvious data entry tasks, we find the admin interface useful in a few other cases:

褰撶劧锛岄櫎浜嗘暟鎹緭鍏ユ柟闈紝鎴戜滑鍙戠幇绠$悊鐣岄潰鍦ㄤ笅闈㈣繖浜涙儏鏅腑涔熸槸寰堟湁鐢ㄧ殑锛

  • Inspecting data models : The first thing we do when weve defined a new model is to call it up in the admin interface and enter some dummy data. This is usually when we find any data modeling mistakes; having a graphical interface to a model quickly reveals problems.

  • 妫鏌ユ暟鎹ā鍨 : 鍦ㄦ垜浠畾涔変簡鏁版嵁妯″瀷鍚庡仛鐨勭涓浠朵簨灏辨槸杈撳叆涓浜涙祴璇曟暟鎹 杩欏彲浠ュ府鍔╂垜浠鏌ユ暟鎹ā鍨嬬殑閿欒锛涙湁涓涓浘褰㈢晫闈㈠彲浠ュ緢蹇殑鍙戠幇閿欒銆

  • Managing acquired data : Theres little actual data entry associated with a site like http://chicagocrime.org , since most of the data comes from an automated source. However, when problems with the automatically acquired data crop up, its useful to be able to go in and edit that data easily.

  • 绠$悊宸茶緭鍏ョ殑鏁版嵁 : 璞 http://chicagocrime.org 杩欐牱鐨勭綉绔欙紝閫氬父鍙湁灏戦儴鍒 鏁版嵁鏄墜宸ヨ緭鍏ョ殑锛屽ぇ閮ㄥ垎鏁版嵁鏄嚜鍔ㄥ鍏ョ殑銆傚鏋滆嚜鍔ㄥ鍏ョ殑鏁版嵁鏈夐棶棰橈紝灏卞彲浠ョ敤绠$悊 鐣岄潰鏉ョ紪杈戝畠銆

Whats Next?

涓嬩竴姝ユ槸浠涔堬紵

So far weve created a few models and configured a top-notch interface for editing data. In the next chapter, well move on to the real meat and potatoes of Web development: form creation and processing.

鐜板湪鎴戜滑宸茬粡鍒涘缓浜嗕竴浜涙ā寮忥紝涓虹紪杈戞暟鎹厤缃簡涓涓《灏栫殑鐣岄潰銆傚湪涓嬩竴绔犻噷锛屾垜浠皢瑕佽浆鍒扮湡姝g殑缃戠珯寮鍙戜笂锛氳〃鍗曠殑鍒涘缓鍜屽鐞嗐

Copyright 2006 Adrian Holovaty and Jacob Kaplan-Moss.
This work is licensed under the GNU Free Document License.
Hosting graciously provided by media temple
Chinese translate hosting by py3k.cn.