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 鐨勮嚜鍔ㄧ鐞嗙晫闈€傝繖涓壒鎬ф槸杩欐牱璧蜂綔鐢ㄧ殑锛氬畠璇诲彇浣犳ā寮忎腑鐨勫厓鏁版嵁锛岀劧鍚庢彁渚涚粰浣犱竴涓己澶ц屼笖鍙互浣跨敤鐨勭晫闈紝缃戠珯绠$悊鑰呭彲浠ョ敤瀹冪珛鍗冲伐浣溿傚湪杩欓噷鎴戜滑灏嗚璁哄浣曟縺娲伙紝浣跨敤鍜屽畾鍒惰繖涓壒鎬с
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爜锛岀幇鍦ㄤ綘鍙互鍦 Publisher 鍜 Author 绫讳腑鍔犲叆 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/
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鎵绀虹殑鐧诲綍灞忓箷銆
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銆
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鎵绀猴紝鏇存敼鍒楄〃涓昏鏄郴缁熷鍍忕殑绱㈠紩椤甸潰銆
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锛夈傚湪浣犳ā寮忎腑瀹氫箟鐨勫煙鍦ㄨ繖閲岄兘鏄剧ず鍑烘潵锛屼綘涔熻娉ㄦ剰鍒颁笉鍚岀被鍨嬬殑鍩熺敤涓嶅悓鐨勬帶浠舵樉绀猴紙濡傦細鏃ユ湡/鏃堕棿鍩熸湁鏃ュ巻鎺т欢锛屽閿敤閫夋嫨鏍忕瓑绛夛級銆
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鎵绀恒
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锛夈
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锛夈
Figure 6-7. Djangos delete confirmation page
鍥 6-7. Django 鍒犻櫎纭椤甸潰
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.
浣犱篃鍙互缁欑粍涓垎閰嶇敤鎴枫備竴涓 缁 绠鍖栦簡缁欑粍涓墍鏈夋垚鍛樺簲鐢ㄤ竴濂楄鍙殑鍔ㄤ綔銆 缁勫湪缁欏ぇ閲忕敤鎴风壒瀹氭潈闄愮殑鏃跺欏緢鏈夌敤銆
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鎵绀恒
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.
閫氳繃浣跨敤杩欎簺閫夐」锛堣繕鏈変竴浜涙槸鍦ㄥ崄浜岀珷鎻忚堪鐨勶級浣犺兘澶熺敤寰堝皯鐨勪唬鐮佸疄鐜板緢寮哄ぇ锛屼骇鍝佺骇鐨勬暟鎹紪杈戠晫闈€
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/ 瀛愮洰褰曢噷鎼滅储 妯℃澘鏉ュ仛鍚庡銆傚叿浣撹鐪嬬鍗佺珷涓殑缂栧啓鑷畾涔夋ā鏉垮姞杞藉櫒绔犺妭銆
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绠$悊绔欑偣鑷畾涔夌殑璇︾粏鍐呭锛岃鍙傜湅绗崄涓冪珷銆
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 杩欐牱鐨勭綉绔欙紝閫氬父鍙湁灏戦儴鍒 鏁版嵁鏄墜宸ヨ緭鍏ョ殑锛屽ぇ閮ㄥ垎鏁版嵁鏄嚜鍔ㄥ鍏ョ殑銆傚鏋滆嚜鍔ㄥ鍏ョ殑鏁版嵁鏈夐棶棰橈紝灏卞彲浠ョ敤绠$悊 鐣岄潰鏉ョ紪杈戝畠銆
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殑缃戠珯寮鍙戜笂锛氳〃鍗曠殑鍒涘缓鍜屽鐞嗐
鍏充簬鏈瘎娉ㄧ郴缁
鏈珯浣跨敤涓婁笅鏂囧叧鑱旂殑璇勬敞绯荤粺鏉ユ敹闆嗗弽棣堜俊鎭備笉鍚屼簬涓鑸鏁寸珷鍋氳瘎娉ㄧ殑鍋氭硶锛 鎴戜滑鍏佽浣犲姣忎竴涓嫭绔嬬殑鈥滄枃鏈潡鈥濆仛璇勬敞銆備竴涓滄枃鏈潡鈥濈湅璧锋潵鏄繖鏍风殑锛
涓涓滄枃鏈潡鈥濇槸涓涓钀斤紝涓涓垪琛ㄩ」锛屼竴娈典唬鐮侊紝鎴栬呭叾浠栦竴灏忔鍐呭銆 浣犻変腑瀹冧細楂樹寒搴︽樉绀:
瑕佸鏂囨湰鍧楀仛璇勬敞锛屼綘鍙渶瑕佺偣鍑诲畠鏃佽竟鐨勬爣璇嗗潡:
鎴戜滑浼氫粩缁嗛槄璇绘瘡涓瘎璁猴紝濡傛灉鍙兘鐨勮瘽鎴戜滑涔熶細鎶婅瘎娉ㄨ冭檻鍒版湭鏉ョ殑鐗堟湰涓幓:
濡傛灉浣犳効鎰忎綘鐨勮瘎娉ㄨ閲囩敤锛岃纭繚鐣欎笅浣犵殑鍏ㄥ悕 (娉ㄦ剰涓嶆槸鏄电О鎴栫畝绉帮級
Many, many thanks to Jack Slocum; the inspiration and much of the code for the comment system comes from Jack's blog, and this site couldn't have been built without his wonderful
YAHOO.ext
library. Thanks also to Yahoo for YUI itself.