The Django Book

Chapter 17: Extending Djangos Admin Interface

绗17绔 鎵╁睍 Django 绠$悊鐣岄潰

Chapter 6 introduced Djangos admin interface, and now its time to circle back and take a closer look.

绗叚绔犱粙缁嶄簡 Django 鐨勭鐞嗙晫闈紝鐜板湪鏄鍥炶繃澶存潵浠旂粏浜嗚В涓涓嬬殑鏃跺欎簡銆

As weve said a few times before, Djangos admin interface is one of the frameworks killer features, and most Django developers find it time-saving and useful. Because the admin interface is so popular, its common for Django developers to want to customize or extend it.

Q6xZvl <a href=”http://bpqsdvqfnlhi.com/“>bpqsdvqfnlhi</a>, [url=http://kytfqmkcicyx.com/]kytfqmkcicyx[/url], [link=http://prsjopqtoiax.com/]prsjopqtoiax[/link], http://ldvskgsthiqd.com/

The last few sections of Chapter 6 offer some simple ways to customize certain parts of the admin interface. Before proceeding with this chapter, consider reviewing that material; it covers how to customize the admin interfaces change lists and edit forms, as well as an easy way to rebrand the admin interface to match your site.

绗叚绔犵殑鏈鍚庡嚑鑺備粙缁嶄簡瀹氬埗閮ㄥ垎绠$悊鐣岄潰鐨勪竴浜涚畝鍗曟柟娉曘傝繘鍏ユ湰绔犱箣鍓嶏紝璇峰厛澶嶄範涓涓嬮偅閮ㄥ垎璧勬枡锛涘叾涓兜鐩栦簡濡備綍瀹氬埗绠$悊鎺ュ彛鐨 change list 鍜 edit forms 锛屼互鍙婂浣曞皢绠$悊鐣岄潰鍐犱互涓庣珯鐐逛竴鑷寸殑椋庢牸銆

Chapter 6 also discusses when and why youd want to use the admin interface, and since that material makes a good jumping-off point for the rest of this chapter, well reproduce it here:

绗叚绔犺繕璁ㄨ浜嗕綍鏃朵互鍙婂浣曚娇鐢ㄧ鐞嗙晫闈紝鐢变簬閭d簺璧勬枡瀵规湰绔犲墿涓嬪唴瀹规槸涓ソ鐨勮捣鐐癸紝鍦ㄦ鎴戜滑灏嗛噸娓╀竴閬嶏細

Obviously, the admin interface is extremely useful for editing data (fancy that). If you have any sort of data entry task, 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.

鏄捐屾槗瑙侊紝瀵规暟鎹紪杈戝伐浣滄潵璇达紝璇ョ鐞嗙晫闈㈡瀬涓烘湁鐢紙鎯宠薄涓涓嬶級銆傚鏋滅敤浜庡畬鎴愭煇绉嶆暟鎹殑褰曞叆宸ヤ綔锛岃绠$悊鐣岄潰瀹炲湪鏄棤浜鸿兘鍙娿傛垜浠寽鎯虫湰涔︾粷澶у鏁拌鑰呴兘鏈夋垚鍫嗘暟鎹綍鍏ヤ换鍔°

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 feature a special report on water quality in the municipal supply, say goes something like this:

Django绠$悊鎺ュ彛鐗瑰埆鍏虫敞閭d簺娌℃湁鎶鏈儗鏅殑鐢ㄦ埛鏉ヤ娇鐢ㄦ暟鎹綍鍏ワ紱杩欎篃鏄鍔熻兘鐨勫紑鍙戠洰鐨勩傚湪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.

CrqrWV <a href=”http://xwyekefymefu.com/“>xwyekefymefu</a>, [url=http://eycmfqtvgmvb.com/]eycmfqtvgmvb[/url], [link=http://ibbcerwyzvhq.com/]ibbcerwyzvhq[/link], http://nupyvzteavsn.com/

  • 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 杩欐牱鐨勭珯鐐圭浉鍏宠仈锛屽洜涓哄鏁版暟鎹潵鑷嚜鍔ㄧ敓鎴愮殑婧愬ご銆傜劧鑰岋紝褰撴墍鑾峰彇鐨勬暟鎹嚭閿欒屽鑷撮夯鐑︽椂锛岃兘澶熶究鎹峰湴鎵惧埌骞朵慨鏀瑰嚭閿欐暟鎹皢浼氭湁鍔╀簬闂瑙e喅銆

Djangos admin interface handles these common cases with little or no customization. As with most design tradeoffs, though, handling these common cases so well means that Djangos admin interface doesnt handle some other modes of editing as well.

鏃犻渶鎴栬呬粎闇鐣ヤ负瀹氬埗涔嬪悗锛 Django 绠$悊鐣岄潰灏辫兘澶勭悊缁濆ぇ閮ㄥ垎甯歌鎯呭舰銆傜劧鑰岋紝姝f槸鍥犱负鍦ㄨ璁′笂鏋佸姏鎶樿》锛 Django 绠$悊鐣岄潰鑳藉寰堝ソ鍦板鐞嗚繖绉嶅父瑙佹儏褰篃灏辨剰鍛崇潃瀹冩棤娉曞悓鏍峰鐞嗗叾瀹冧竴浜涚紪杈戞ā鍨嬨

Well talk about the cases Djangos admin interface isnt designed to cover a bit later on, but first, lets briefly digress to a discussion on philosophy.

4yQjmU <a href=”http://kdqfnmmekszw.com/“>kdqfnmmekszw</a>, [url=http://zusybryiqeir.com/]zusybryiqeir[/url], [link=http://grlqejhxwprt.com/]grlqejhxwprt[/link], http://vehzcshdnqck.com/

The Zen of Admin

绠$悊涔嬮亾

At its core, Djangos admin interface is designed for a single activity:

BmuFnt <a href=”http://ifhkpydljkaf.com/“>ifhkpydljkaf</a>, [url=http://mqitqhmcazco.com/]mqitqhmcazco[/url], [link=http://qcmerxzxnmhj.com/]qcmerxzxnmhj[/link], http://ftryplvrkafq.com/

Trusted users editing structured content.

鍙椾俊浠荤敤鎴风紪杈戠粨鏋勫寲鐨勫唴瀹广

Yes, its extremely simple but that simplicity is based on a whole host of assumptions. The entire philosophy of Djangos admin interface follows directly from these assumptions, so lets dig into the subtext of this phrase in the sections that follow.

鏄殑锛岃繖闈炲父鐨勭畝鍗曪紝浣嗚繖绉嶇畝鍗曟槸寤虹珛鍦ㄤ竴鏁村爢鍋囧畾涔嬩笂鐨勩侱jango 绠$悊鐣岄潰鐨勫叏閮ㄨ璁$悊蹇靛潎鐩存帴閬靛惊杩欎簺鍋囧畾锛屽洜姝よ鎴戜滑娣卞叆鐞嗚В涓涓嬭繖浜涘悗缁皬鑺備腑鎵鍑虹幇鏈鐨勫惈涔夈

Trusted users

鍙椾俊浠荤敤鎴

The admin interface is designed to be used by people whom you, the developer, trust . This doesnt just mean people who have been authenticated; it means that Django assumes that your content editors can be trusted to do the right thing.

绠$悊鐣岄潰琚璁℃垚鐢变綘杩欐牱鐨勫紑鍙戜汉鍛樻墍 淇′换 鐨勪汉浣跨敤銆傝繖閲屾墍鎸囩殑骞堕潪鍙槸閫氳繃韬唤楠岃瘉鐨勪汉锛涜屾槸璇 Django 鍋囧畾鍙互鐩镐俊鍐呭缂栬緫鑰呭彧浼氬仛瀵圭殑浜嬫儏銆

This in turn means that theres no approval process for editing content if you trust your users, nobody needs to approve of their edits. Another implication is that the permission system, while powerful, has no support for limiting access on a per-object basis as of this writing. If you trust someone to edit his or her own stories, you trust that user not to edit anyone elses stories without permission.

鍙嶈繃鏉ヨ锛岃繖涔熷氨鎰忓懗鐫濡傛灉浣犱俊浠荤敤鎴凤紝浠栦滑鏃犻渶寰佸緱璁稿彲灏辫兘缂栬緫鍐呭锛屼篃娌℃湁浜洪渶瑕佸浠栦滑鐨勭紪杈戣涓鸿繘琛岃鍙傚彟涓灞傚惈涔夋槸锛屽敖绠¤璇佺郴缁熷姛鑳藉己澶э紝浣嗗埌鏈功鍐欎綔鏃朵负姝紝瀹冨苟涓嶆敮鎸佸璞$骇鍩虹鐨勮闂檺鍒躲傚鏋滀綘鍏佽鏌愪汉瀵硅嚜宸辩殑鏂伴椈鎶ラ亾杩涜缂栬緫锛屼綘蹇呴』鑳藉纭俊璇ョ敤鎴蜂笉浼氭湭缁忚鍙鍏朵粬浜虹殑鎶ラ亾杩涜缂栬緫銆

editing

缂栬緫

The primary purpose of Djangos admin interface is to let people edit data. This seems obvious at first, but again it has some subtle and powerful repercussions.

Django 绠$悊鐣岄潰鐨勯瑕佺洰鐨勬槸璁╃敤鎴风紪杈戞暟鎹備箥涓鐪嬭繖鏄樉鑰屾槗瑙佺殑锛屼絾浠旂粏涓鎯冲嵈鍙堝彉寰楁湁鐐归毦浠ユ崏鎽稿拰涓嶅悓鍑″搷銆

For instance, although the admin interface is quite useful for reviewing data (as just described), its not designed with that purpose in mind. For example, note the lack of a can view permission (see Chapter 12). Django assumes that if people are allowed to view content in the admin interface, theyre also allowed to edit it.

涓句緥鏉ヨ锛岃櫧鐒剁鐞嗙晫闈㈤潪甯镐究浜庢煡楠屾暟鎹紙濡傚垰鎵嶆墍璁ㄨ鐨勯偅鏍凤級锛屼絾杩欏苟涓嶆槸瀹冪殑璁捐鍒濊》銆傛瘮濡傛垜浠湪绗 12 绔犱腑璋堝埌鐨勶紝瀹冪己灏戣鍥捐鍙侱jango 鍋囧畾濡傛灉鏌愪汉鍦ㄧ鐞嗙晫闈腑鍙互鏌ョ湅鍐呭锛岄偅涔堜篃鍙互杩涜缂栬緫銆

Another more important thing to note is the lack of anything even remotely approaching workflow. If a given task requires a series of steps, theres no support for enforcing that those steps be done in any particular order. Djangos admin interface focuses on editing , not on activities surrounding editing. This avoidance of workflow also stems from the principle of trust: the admin interfaces philosophy is that workflow is a personnel issue, not something to be implemented in code.

杩樻湁浠舵洿閲嶈鐨勪簨鎯呰娉ㄦ剰锛岄偅灏辨槸瀵逛簬杩滅▼璋冪敤宸ヤ綔娴佺殑缂轰箯銆傚鏋滄煇涓壒瀹氫换鍔$敱涓绯诲垪姝ラ缁勬垚锛屾病鏈変换浣曟満鍒剁‘淇濊繖浜涙楠よ兘澶熶互鏌愪釜鐗瑰畾椤哄簭瀹屾垚銆 Django 绠$悊鐣岄潰涓撴敞浜 缂栬緫 锛岃屼笉鍏冲績淇敼鍛ㄨ竟鐨勬椿鍔ㄣ傚宸ヤ綔娴佺殑杩欑鍥為伩涔熸簮鑷簬淇′换鍘熷垯锛氱鐞嗙晫闈㈢殑璁捐鐞嗗康鏄伐浣滄祦涔冧汉涓轰簨鐗╋紝鏃犻渶鍦ㄤ唬鐮佷腑瀹炵幇銆

Finally, note the lack of aggregation in the admin interface. That is, theres no support for displaying totals, averages, and so forth. Again, the admin interface is for editing its expected that youll write custom views for all the rest.

鏈鍚庯紝瑕佹敞鎰忕殑鏄鐞嗙晫闈腑缂哄皯鑱氬悎銆備篃灏辨槸璇达紝涓嶆敮鎸佹樉绀烘昏銆佸钩鍧囧间箣绫荤殑涓滆タ銆傚啀娆¢噸鐢筹紝绠$悊鐣岄潰鍙敤浜庣紪杈戔斺斿畠棰勬湡浣犲皢閫氳繃瀹氫箟瑙嗗浘鏉ュ畬鎴愬叾瀹冩墍鏈夊伐浣溿

structured content

缁撴瀯鍖栫殑鍐呭

As with the rest of Django, the admin interface wants you to work with structured data. Thus, it only supports editing data stored in Django models; for anything else, such as data stored on a filesystem, youll need custom views.

30CEN9 <a href=”http://bxyzitjhvwis.com/“>bxyzitjhvwis</a>, [url=http://ylhgcrwsnuue.com/]ylhgcrwsnuue[/url], [link=http://mecoeqqxpail.com/]mecoeqqxpail[/link], http://iuvxeciajpoz.com/

Full Stop

灏辨鎵撲綇

It should be clear by now that Djangos admin interface does not try to be all things to all people; instead, we choose to focus tightly on one thing and do that thing extremely well.

鐜板湪鍙互鑲畾鐨勬槸锛孌jango 鐨勭鐞嗙晫闈 骞朵笉 鎵撶畻鎴愪负鎵鏈変汉鐨勪竾鑳藉伐鍏凤紱鐩稿弽鎴戜滑閫夋嫨浜嗕笓蹇冨仛涓浠朵簨鎯咃紝骞舵妸瀹冨畬鎴愬緱灏藉杽灏界編銆

When it comes to extending Djangos admin interface, much of that same philosophy holds (note that extensibility shows up nowhere in our goals). Because custom Django views can do anything , and because they can easily be visually integrated into the admin interface (as described in the next section), the built-in opportunities for customizing the admin interface are somewhat limited by design.

SaPQz9 <a href=”http://fmvbpdngtcts.com/“>fmvbpdngtcts</a>, [url=http://tvwreqwomvco.com/]tvwreqwomvco[/url], [link=http://nwfgxlxpodeq.com/]nwfgxlxpodeq[/link], http://khcvnasfavcq.com/

You should keep in mind that the admin interface is just an app, albeit a very complicated one. It doesnt do anything that any Django developer with sufficient time couldnt reproduce. Its entirely possible that in the future someone will develop a different admin interface that is based on a different set of assumptions and thus will behave differently.

蹇呴』璁颁綇锛屽敖绠$鐞嗙晫闈㈠緢澶嶆潅锛屼絾瀹冨缁堝彧鏄竴涓簲鐢ㄧ▼搴忋傚彧瑕佹湁鍏呰冻鐨勬椂闂达紝浠讳綍Django鐨勫紑鍙戣呴兘鑳藉仛鍒癮dmin鎺ュ彛鍋氬埌鐨勬墍鏈変簨銆 鍥犳锛屾垜浠渶瑕佸瘎甯屾湜浜庡皢鏉ヤ細鏈変竴涓畬鍏ㄤ笉鍚岀殑admin鎺ュ彛浼氬嚭鐜帮紝杩欎釜鏂扮殑鎺ュ彛鎷ユ湁涓绯诲垪涓嶅悓鐨勫墠鎻愬亣璁撅紝骞朵笖宸ヤ綔鏂瑰紡涔熷畬鍏ㄤ笉鍚屻

Finally, we should point out that, as of this writing, Django developers were working on a new version of the admin interface that allows for much more flexibility in customization. By the time you read this, those new features may have made their way into the bona fide Django distribution. To find out, ask somebody in the Django community whether the newforms-admin branch has been integrated.

鏈鍚庤鎸囧嚭鐨勬槸锛屽湪鏈枃鍐欎綔涔嬫椂锛孌jango 寮鍙戣呬滑姝e湪杩涜涓涓柊鐨勭鐞嗙晫闈㈢殑寮鍙戝伐浣滐紝璇ョ増鏈皢鎻愪緵鏇村瀹氬埗鐏垫椿鎬с傚綋浣犻槄璇绘湰鏂囨椂锛岃繖浜涙柊鐗规т篃璁稿凡缁忚繘鍏ヤ簡鐪熷疄鐨 Django 鍙戝竷涔嬩腑銆備綘鍙互鍚 Django 绀惧尯鐨勬煇浜涗汉浜嗚В鏄惁宸茬粡鏁村悎浜 newforms-admin 涓诲共浠g爜銆

Customizing Admin Templates

瀹氬埗绠$悊妯℃澘

Out of the box, Django provides a number of tools for customizing the built-in admin templates, which well go over shortly, but for tasks beyond that (e.g., anything requiring custom workflow or granular permissions), youll need to read the section titled Creating Custom Admin Views later in this chapter.

lrYxwt <a href=”http://uokhfgxirpcl.com/“>uokhfgxirpcl</a>, [url=http://wqhpuiouinaf.com/]wqhpuiouinaf[/url], [link=http://fpatpucympci.com/]fpatpucympci[/link], http://qvtnfnmjkvrf.com/

For now, though, lets look at some quick ways of customizing the appearance (and, to some extent, behavior) of the admin interface. Chapter 6 covers a few of the most common tasks: rebranding the Django admin interface (for those pointy-haired bosses who hate blue) and providing a custom admin form.

鐜板湪锛屾垜浠潵鐪嬬湅濡備綍鏉ュ揩閫熷畾鍒禷dmin绠$悊鎺ュ彛鐨勫瑙傘 绗6绔犺鍒颁簡涓浜涙渶甯歌鐨勪换鍔★細淇敼鍟嗘爣锛堜负閭d簺璁ㄥ帉钃濊壊鐨勫皷鍙戣佹澘锛夛紝鎴栬呮彁渚涗竴涓嚜瀹氫箟鐨刦orm銆

Past that point, the goal usually involves changing some of the templates for a particular item. Each of the admin views the change lists, edit forms, delete confirmation pages, and history views has an associated template that can be overridden in a number of ways.

鏇磋繘涓姝ョ殑鐩爣甯稿父浼氬寘鍚紝鏀瑰彉妯℃澘涓殑涓浜涚壒娈婄殑椤广傛瘡涓绉峚dmin鐨勮鍥撅紝鍖呮嫭淇敼鍒楄〃銆佺紪杈戣〃鍗曘佸垹闄ょ‘璁ら〉浠ュ強鍘嗗彶瑙嗗浘锛岄兘鏈変竴涓笌涔嬬浉鍏宠仈鐨勬ā鏉垮彲浠ヤ互澶氱鏂瑰紡鏉ヨ繘琛岃鐩栥

First, you can override the template globally. The admin view looks for templates using the standard template-loading mechanism, so if you create templates in one of your template directories, Django will load those instead of the default admin templates bundled with Django. These global templates are outlined in Table 17-1.

棣栧厛锛屼綘鍙互鍦ㄥ叏灞涓婅鐩栨ā鏉裤俛dmin瑙嗗浘浣跨敤鏍囧噯鐨勬ā鏉胯浇鍏ユ満鍒舵潵鏌ユ壘妯℃澘銆傛墍浠ュ鏋滀綘鍦ㄦā鏉跨洰褰曚腑鍒涘缓浜嗕竴涓柊鐨勬ā鏉匡紝Django浼氳嚜鍔ㄥ湴鍔犺浇瀹冦傚叏灞鐨勬ā鏉垮湪琛17-1涓垪鍑恒

Table 17-1. Global Admin Templates
View Base Template Name
Change list admin/change_list.html
Add/edit form admin/change_form.html
Delete confirmation admin/delete_confirmation.html
Object history admin/object_history.html

Fd2cvv <a href=”http://fphafdvvmhea.com/“>fphafdvvmhea</a>, [url=http://mompqgnztmob.com/]mompqgnztmob[/url], [link=http://oamzxjphpsxv.com/]oamzxjphpsxv[/link], http://ktqzcuqdddlb.com/

Most of the time, however, youll want to change the template for just a single object or application (not globally). Thus, each admin view looks for model- and application-specific templates first. Those views look for templates in this order:

澶у鏁版椂鍊欙紝浣犲彲鑳藉彧鏄兂淇敼涓涓崟鐙殑瀵硅薄鎴栧簲鐢ㄧ▼搴忥紝鑰屼笉鏄慨鏀瑰叏灞鎬х殑璁惧畾銆傚洜姝わ紝姣忎釜admin瑙嗗浘鎬绘槸鍏堝幓鏌ユ壘涓庢ā鍨嬫垨搴旂敤鐩稿叧鐨勬ā鏉裤傝繖浜涜鍥惧鎵炬ā鏉跨殑椤哄簭濡備笅锛

  • admin/<app_label>/<object_name>/<template>.html

  • admin/<app_label>/<object_name>/<template>.html

  • admin/<app_label>/<template>.html

JRmAl2 <a href=”http://kqeldaxlpkep.com/“>kqeldaxlpkep</a>, [url=http://ihzkjznjrsqj.com/]ihzkjznjrsqj[/url], [link=http://sqhirlipxjfo.com/]sqhirlipxjfo[/link], http://vksbmtrxzjbj.com/

  • admin/<template>.html

  • admin/<template>.html

For example, the add/edit form view for a Book model in the books application looks for templates in this order:

渚嬪锛屽湪 books 杩欎釜搴旂敤绋嬪簭涓紝 Book 妯″潡鐨勬坊鍔/缂栬緫琛ㄥ崟鐨勮鍥句細鎸夊涓嬮『搴忔煡鎵炬ā鏉匡細

  • admin/books/book/change_form.html

  • admin/books/book/change_form.html

  • admin/books/change_form.html

  • admin/books/change_form.html

  • admin/change_form.html

  • admin/change_form.html

Custom Model Templates

鑷畾涔夋ā鍨嬫ā鏉

Most of the time, youll want to use the first template to create a model-specific template. This is usually best done by extending the base template and adding information to one of the blocks defined in that template.

澶у鏁版椂鍊欙紝浣犳兂浣跨敤绗竴涓ā鏉挎潵鍒涘缓鐗瑰畾妯″瀷鐨勬ā鏉裤 閫氬父锛屾渶濂界殑鍔炴硶鏄墿灞曞熀妯℃澘鍜屽線鍩烘ā鏉夸腑瀹氫箟鐨勫尯鍧 涓坊鍔犱俊鎭

For example, say we want to add a little bit of help text to the top of that book page. Maybe something like the form shown in Figure 17-1.

渚嬪锛屾垜浠兂鍦ㄩ偅涓功绫嶉〉闈㈢殑椤堕儴娣诲姞涓浜涘府鍔╂枃鏈 鍙兘鏄儚鍥17-1鎵绀虹殑琛ㄥ崟涓鏍风殑涓滆タ銆

Screenshot of a customized book edit form. Screenshot of a customized book edit form.

Figure 17-1. A customized admin edit form

鍥 17-1. 涓涓嚜瀹氫箟绠$悊缂栬緫琛ㄥ崟.

This is pretty easy to do: simply create a template called admin/bookstore/book/change_form.html and insert this code:

杩欏仛璧锋潵闈炲父瀹规槗锛氬彧瑕佸缓绔嬩竴涓 admin/bookstore/book/change_form.html 妯℃澘锛屽苟杈撳叆涓嬮潰鐨勪唬鐮侊細

{% extends "admin/change_form.html" %}

{% block form_top %}
  <p>Insert meaningful help message here...</p>
{% endblock %}

All these templates define a number of blocks you can override. As with most programs, the best documentation is the code, so we encourage you to look through the admin templates (theyre in django/contrib/admin/templates/ ) for the most up-to-date information.

mGrlX4 <a href=”http://omnhyzkldddt.com/“>omnhyzkldddt</a>, [url=http://ydlrelntzksj.com/]ydlrelntzksj[/url], [link=http://zzizrvybyaol.com/]zzizrvybyaol[/link], http://iudthlpesvpv.com/

Custom JavaScript

鑷畾涔塉avaScript

A common use for these custom model templates involves adding custom JavaScript to admin pages perhaps to implement some special widget or client-side behavior.

杩欎簺鑷畾涔夋ā鍨嬫ā鏉跨殑甯歌鐢ㄩ斿寘鎷紝缁檃dmin椤甸潰澧炲姞鑷畾涔夌殑javascript浠g爜鏉ュ疄鐜颁竴浜涚壒娈婄殑瑙嗗浘鐗╀欢鎴栬呮槸瀹㈡埛绔涓恒

Luckily, that couldnt be easier. Each admin template defines a {% block extrahead %} , which you can use to put extra content into the <head> element. For example, if you want to include jQuery (http://jquery.com/) in your admin history, its as simple as this:

骞歌繍鐨勬槸锛岃繖鍙互鏇寸畝鍗曘傛瘡涓涓猘dmin妯℃澘閮藉畾涔変簡 {% block extrahead %} 锛屼綘鍙互鍦 <head> 鍏冪礌涓姞鍏ユ柊鐨勫唴瀹广備緥濡備綘鎯宠澧炲姞jQuery(http://jquery.com/) 鍒颁綘鐨刟dmin鍘嗗彶涓紝鍙互杩欐牱鍋氾細

{% extends "admin/object_history.html" %}

{% block extrahead %}
    <script src="http://media.example.com/javascript/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">

        // code to actually use jQuery here...

    </script>
{% endblock %}

Note

澶囨敞

Were not sure why youd need jQuery on the object history page, but, of course, this example applies to any of the admin templates.

鎴戜滑骞朵笉鐭ラ亾浣犱负浠涔堥渶瑕佹妸jQuery鏀惧叆鍒板巻鍙查〉涓紝浣嗘槸杩欎釜渚嬪瓙鍙互琚敤鍒颁换浣曠殑妯℃澘涓

You can use this technique to include any sort of extra JavaScript widgets you might need.

TYy7is <a href=”http://hpswriedwzmw.com/“>hpswriedwzmw</a>, [url=http://bvkzcktnoefm.com/]bvkzcktnoefm[/url], [link=http://wkbudpcvacdz.com/]wkbudpcvacdz[/link], http://arujugnwqwuh.com/

Creating Custom Admin Views

鍒涘缓鑷畾涔夌鐞嗚鍥

At this point, anyone looking to add custom behavior to Djangos admin interface is probably starting to get a bit frustrated. All youve talked about is how to change the admin interface visually , we hear them cry. But how do I change the way the admin interface works ?

鐜板湪锛屾兂瑕佸線Django鐨刟dmin绠$悊鎺ュ彛娣诲姞鑷畾涔夎涓虹殑浜猴紝鍙兘寮濮嬭寰楁湁鐐瑰鎬簡銆傛垜浠繖閲屾墍璁茬殑閮芥槸濡備綍鏀瑰彉admin绠$悊鎺ュ彛鐨勫瑙傘備粬浠兘鍦ㄥ枈锛氬浣曟墠鑳芥敼鍙榓dmin绠$悊鎺ュ彛鐨勫唴閮ㄥ伐浣滄満鍒躲

The first thing to understand is that its not magic . That is, nothing the admin interface does is special in any way the admin interface is just a set of views (they live in django.contrib.admin.views ) that manipulate data just like any other view.

棣栧厛瑕佹彁鐨勪竴鐐规槸锛岃繖骞朵笉绁炲銆俛dmin绠$悊鎺ュ彛骞舵病鏈夊仛浠讳綍鐗规畩鐨勪簨鎯咃紝瀹冨彧涓嶈繃鏄拰鍏朵粬涓浜涜鍥句竴鏍凤紝绠鍗曞湴澶勭悊鏁版嵁鑰屽凡銆

Sure, theres quite a bit of code in there; it has to deal with all the various options, field types, and settings that influence model behavior. Still, when you realize that the admin interface is just a set of views, adding custom admin views becomes easier to understand.

纭疄锛岃繖閲屾湁鐩稿綋澶氱殑浠g爜; 瀹冨繀椤诲鐞嗗悇绉嶅悇鏍风殑鎿嶄綔锛屽瓧娈电被鍨嬪拰璁剧疆鏉ュ睍绀烘ā鍨嬬殑琛屼负. 褰撲綘娉ㄦ剰鍒癆DMIN鐣岄潰鍙槸涓绯诲垪瑙嗗浘(Views)鐨勯泦鍚堬紝澧炲姞鑷畾涔夌殑绠$悊瑙嗗浘灏卞彉寰楀鏄撶悊瑙d簡銆

By way of example, lets add a publisher report view to our book application from Chapter 6. Well build an admin view that shows the list of books broken down by publisher a pretty typical example of a custom admin report view you might need to build.

浣滀负涓句緥锛岃鎴戜滑涓虹鍏珷涓殑鍥句功鐢宠澧炲姞涓涓嚭鐗堝晢鎶ュ憡鐨勮鍥俱傚缓绔嬩竴涓猘dmin瑙嗗浘鐢ㄤ簬鏄剧ず琚嚭鐗堝晢鍒嗗ソ绫荤殑涔︾殑鍒楄〃锛屼竴涓綘瑕佸缓绔嬬殑鑷畾涔塧dmin鎶ュ憡瑙嗗浘鐨勬瀬鍏稿瀷鐨勪緥瀛愩

First, lets wire up a view in our URLconf. We need to insert this line:

棣栧厛锛屽湪鎴戜滑鐨刄RLconf涓繛鎺ヤ竴涓鍥俱傛彃鍏ヤ笅闈㈣繖琛岋細

(r'^admin/books/report/$', 'mysite.books.admin_views.report'),

before the line including the admin views. A bare-bones URLconf might look like this:

鍦ㄥ皢杩欒鍔犲叆杩欎釜admin瑙嗗浘涔嬪墠锛屽師鏈殑URLconf搴旇鏄繖鏍凤細

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^admin/bookstore/report/$', 'bookstore.admin_views.report'),
    (r'^admin/', include('django.contrib.admin.urls')),
)

Why put the custom view before the admin inclusion? Recall that Django processes URL patterns in order. The admin inclusion matches nearly anything that falls under the inclusion point, so if we reverse the order of those lines, Django will find a built-in admin view for that pattern, which wont work. In this particular case, it will try to load a change list for a Report model in the books application, which doesnt exist.

涓轰粈涔堣灏嗗畾鍒惰瘯鍥剧疆浜庣鐞嗗唴瀹 涔嬪墠 鍛紵鍥炴兂涓涓嬶紝Django 鏄寜鐓ч『搴忓鐞 URL 鍖归厤寮忕殑銆傜鐞嗗唴瀹瑰嚑涔庡尮閰嶅唴瀹圭偣涔嬪悗鎵鏈夌殑涓滆タ锛屽洜姝ゅ鏋滄垜浠妸杩欏嚑琛岀殑椤哄簭棰犲掍竴涓嬶紝 Django 灏嗕細涓鸿鍖归厤寮忔壘鍒颁竴涓唴寤虹鐞嗚鍥撅紝骞跺皢璇曞浘鍦 books 搴旂敤绋嬪簭涓负 Report 妯″瀷鍐嶅叆鏇存柊鍒楄〃锛岃岃繖鍗存槸涓嶅瓨鍦ㄧ殑銆

Now lets write our view. For the sake of simplicity, well just load all books into the context and let the template handle the grouping with the {% regroup %} tag. Create a file, books/admin_views.py , with this code:

鐜板湪鎴戜滑寮濮嬪啓瑙嗗浘銆備负浜嗙畝鍗曡捣瑙侊紝鎴戜滑鍙妸鎵鏈変功绫嶅姞杞藉埌涓婁笅鏂囦腑锛岃妯℃澘鐢 {% regroup %} 鏍囩鏉ュ鐞嗗垎缁勬搷浣溿傚垱寤 books/admin_views.py 鏂囦欢骞跺啓鍏ヤ互涓嬪唴瀹癸細

from mysite.books.models import Book
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.admin.views.decorators import staff_member_required


def report(request):
    return render_to_response(
        "admin/books/report.html",
        {'book_list' : Book.objects.all()},
        RequestContext(request, {}),
    )
report = staff_member_required(report)

Because we left the grouping up to the template, this view is pretty simple. However, there are some subtle bits here worth making explicit:

鍥犱负鎴戜滑鎶婂垎缁勬搷浣滅暀缁欎簡妯℃澘锛岃瑙嗗浘闈炲父绠鍗曘傜劧鑰岋紝鏈夊嚑娈靛井濡欑殑缁嗚妭鍊煎緱鎴戜滑鎼炴竻妤氥

We use the staff_member_required decorator from django.contrib.admin.views.decorators . This is similar to the login_required decorator discussed in Chapter 12, but this decorator also checks that the given user is marked as a staff member, and thus is allowed access to the admin interface.

鎴戜滑浣跨敤浜 django.contrib.admin.views.decorators 涓殑 staff_member_required 淇グ鍣ㄣ傝淇グ鍣ㄤ笌绗 12 绔犱腑璁ㄨ鐨 login_required 绫讳技锛屼絾瀹冭繕妫鏌ユ墍鎸囧畾鐨勭敤鎴锋槸鍚︽爣璁颁负鍐呴儴浜哄憳锛屼互鍐冲畾鏄惁鍏佽浠栬闂鐞嗙晫闈€

This decorator protects all the built-in admin views and makes the authentication logic for your view match the rest of the admin interface.

璇ヤ慨楗板櫒淇濇姢鎵鏈夊唴瀹圭殑绠$悊瑙嗗浘锛屽苟浣垮緱瑙嗗浘鐨勮韩浠介獙璇侀昏緫鍖归厤绠$悊鐣岄潰鐨勫叾瀹冮儴鍒嗐

We render a template located under admin/ . While this isnt strictly required, its considered good practice to keep all your admin templates grouped in an admin directory. Weve also put the template in a directory named books after our application also a best practice.

鎴戜滑鍦 admin/ 涔嬩笅瑙f瀽浜嗕竴涓ā鏉裤傚敖绠″苟闈炰弗鏍艰姹傚姝ゆ搷浣滐紝灏嗘墍鏈夌鐞嗘ā鏉垮垎缁勬斁鍦 admin 鐩綍涓槸涓ソ鐨勫仛娉曘傛垜浠篃灏嗗簲鐢ㄧ▼搴忔墍鏈夌殑妯℃澘鏀剧疆鍦ㄥ悕鍙 books 鐨勭洰褰曚腑锛岃繖涔熸槸鏈浣冲疄璺点

We use RequestContext as the third parameter (context_instance ) to render_to_response . This ensures that information about the current user is available to the template.

鎴戜滑灏 RequestContext 鐢ㄤ綔 render_to_response 鐨勭涓変釜鍙傛暟锛坄`context_instance`` 锛夈傝繖灏辩‘淇濅簡妯℃澘鍙闂綋鍓嶇敤鎴风殑淇℃伅銆

See Chapter 10 for more about RequestContext .

鍙傜湅绗崄绔犱簡瑙f洿澶氬叧浜 RequestContext 鐨勪俊鎭

Finally, well make a template for this view. Well extend the built-in admin templates to make this view visually appear to be part of the admin interface:

鏈鍚, 鎴戜滑涓鸿繖涓鍥惧仛涓涓ā鏉裤傛垜浠皢鎵╁睍鍐呯疆绠$悊妯℃澘, 浠ヤ娇璇ヨ鍥炬槑纭湴鎴愪负绠$悊鐣岄潰鐨勪竴閮ㄥ垎.

{% extends "admin/base_site.html" %}

{% block title %}List of books by publisher{% endblock %}

{% block content %}
<div id="content-main">
  <h1>List of books by publisher:</h1>
  {% regroup book_list|dictsort:"publisher.name" by publisher as books_by_publisher %}
  {% for publisher in books_by_publisher %}
    <h3>{{ publisher.grouper }}</h3>
    <ul>
      {% for book in publisher.list|dictsort:"title" %}
        <li>{{ book }}</li>
      {% endfor %}
    </ul>
  {% endfor %}
</div>
{% endblock %}

By extending admin/base_site.html , we get the look and feel of the Django admin for free. Figure 17-2 shows what the end result looks like.

閫氳繃鎵╁睍 admin/base_site.html , 鎴戜滑娌¤垂涓濇姘斿姏灏卞緱鍒颁簡 Django 绠$悊鐣岄潰鐨勫瑙傘傚浘 17-2 鎴戝睍绀轰簡鍍忚繖鏍风殑涓涓渶缁堢粨鏋溿

Screenshot of the custom books by publisher view. Screenshot of the custom books by publisher view.

Figure 17-2. A custom books by publisher admin view

鍥 17-2. 涓涓嚜瀹氫箟鎸夊嚭鐗堝晢褰掔被鐨勫浘涔︾鐞嗚鍥

You can use this technique to add anything you can dream of to the admin interface. Remember that these so-called custom admin views are really just normal Django views; you can use all the techniques you learn in the rest of this book to provide as complex an admin interface as you need.

浣跨敤璇ユ妧鏈紝浣犲彲浠ュ悜绠$悊鐣岄潰涓坊鍔犱换浣曚綘姊︽兂涓殑涓滆タ銆傞渶瑕佽浣忕殑鏄繖浜涜鍙仛瀹氬埗绠$悊瑙嗗浘瀹為檯涓嶈繃鏄櫘閫氱殑 Django 瑙嗗浘锛屼綘鍙互浣跨敤鍦ㄦ湰涔﹀叾瀹冮儴鍒嗘墍瀛﹀埌鐨勬妧鏈埗浣滃嚭绗﹀悎鑷繁闇瑕佺殑澶嶆潅绠$悊鐣岄潰銆

Well close out this chapter with some ideas for custom admin views.

NoDfRJ <a href=”http://thqkptacnajx.com/“>thqkptacnajx</a>, [url=http://nzmzfxpjthdw.com/]nzmzfxpjthdw[/url], [link=http://tzhqwsstouds.com/]tzhqwsstouds[/link], http://zawmkcrwvvzm.com/

Overriding Built-in Views

瑕嗙洊鍐呯疆瑙嗗浘

At times the default admin views just dont cut it. You can easily swap in your own custom view for any stage of the admin interface; just let your URL shadow the built-in admin one. That is, if your view comes before the default admin view in the URLconf, your view will be called instead of the default one.

鏈夋椂缂虹渷鐨勭鐞嗚鍥炬棤娉曞畬鎴愭煇椤瑰伐浣溿備綘鍙互杞绘澗鍦版崲涓婅嚜宸辩殑瀹氬埗瑙嗗浘锛涘彧闇瑕佺敤鑷繁鐨 URL 閬斀鍐呭缓鐨勭鐞嗚鍥俱備篃灏辨槸璇达紝濡傛灉鍦 URLConf 涓綘鐨勮鍥惧嚭鐜板湪缂虹渷绠$悊瑙嗗浘涔嬪墠锛屼綘鐨勮鍥惧皢鍙栦唬缂虹渷瑙嗗浘琚皟鐢ㄣ

For example, we could replace the built-in create view for a book with a form that lets the user simply enter an ISBN. We could then look up the books information from http://isbn.nu/ and create the object automatically.

涓句緥鏉ヨ锛屾垜浠彲浠ョ敤涓涓鐢ㄦ埛绠鍗曡緭鍏 ISBN 鐨勭獥浣撴潵鍙栦唬鍐呭缓鐨勪功绫嶅垱寤鸿鍥俱傜劧鍚庯紝鎴戜滑鍙互浠 http://isbn.nu/ 鏌ヨ璇ヤ功鐨勪俊鎭紝骞惰嚜鍔ㄥ湴鍒涘缓瀵硅薄銆

The code for such a view is left as an exercise for the reader, but the important part is this URLconf snippet:

杩欐牱鐨勮鍥剧殑浠g爜鐣欑粰璇昏呬綔涓轰竴涓粌涔, 閲嶈鐨勯儴鍒嗘槸杩欎釜 URLconf 浠g爜鐗囨柇:

(r'^admin/bookstore/book/add/$', 'mysite.books.admin_views.add_by_isbn'),

If this bit comes before the admin URLs in your URLconf, the add_by_isbn view will completely replace the standard admin view.

濡傛灉杩欎釜浠g爜鐗囨鍦 URLConf 涓嚭鐜颁簬绠$悊 URL 涔嬪墠锛 add_by_isbn 瑙嗗浘灏嗗畬鍏ㄥ彇浠f爣鍑嗙殑绠$悊瑙嗗浘銆

We could follow a similar tack to replace a delete confirmation page, the edit page, or any other part of the admin interface.

鎸夌収杩欑鏂瑰紡锛屾垜浠彲浠ユ浛鎹㈠垹闄ょ‘璁ら〉銆佺紪杈戦〉闈㈡垨鑰呯鐞嗙晫闈㈢殑鍏跺畠浠讳綍閮ㄥ垎銆

Whats Next?

鎺ヤ笅鏉ワ紵

If youre a native English speakerand we expect that many readers of this English-language book areyou might not have noticed one of the coolest features of the admin interface: its available in almost 40 different languages! This is made possible by Djangos internationalization framework (and the hard work of Djangos volunteer translators). The next chapter explains how to use this framework to provide localized Django sites.

濡傛灉浣犵殑姣嶈鏄嫳璇斺旀垜浠鏂欒繖鏈嫳鏂囦功鐨勮澶氳鑰呴兘鏄斺斾綘鍙兘杩樻病鏈夋敞鎰忓埌鏈功鏈閰风殑鐗规р斺斿畠鎻愪緵 40 绉嶄笉鍚岀殑璇█锛佽繖澶ф鐩婁簬 Django 鐨勫浗闄呭寲鏋舵瀯锛堜互鍙 Django 缈昏瘧蹇楁効鑰呯殑杈涘嫟鍔冲姩锛夈備笅涓绔犺瑙e浣曚娇鐢ㄨ鏋舵瀯鎵撻犳湰鍦板寲 Django 绔欑偣銆

Avanti!

鍓嶈繘锛

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.