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/
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 绠$悊鐣岄潰鐨勫叏閮ㄨ璁$悊蹇靛潎鐩存帴閬靛惊杩欎簺鍋囧畾锛屽洜姝よ鎴戜滑娣卞叆鐞嗚В涓涓嬭繖浜涘悗缁皬鑺備腑鎵鍑虹幇鏈鐨勫惈涔夈
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.
鍙嶈繃鏉ヨ锛岃繖涔熷氨鎰忓懗鐫濡傛灉浣犱俊浠荤敤鎴凤紝浠栦滑鏃犻渶寰佸緱璁稿彲灏辫兘缂栬緫鍐呭锛屼篃娌℃湁浜洪渶瑕佸浠栦滑鐨勭紪杈戣涓鸿繘琛岃鍙傚彟涓灞傚惈涔夋槸锛屽敖绠¤璇佺郴缁熷姛鑳藉己澶э紝浣嗗埌鏈功鍐欎綔鏃朵负姝紝瀹冨苟涓嶆敮鎸佸璞$骇鍩虹鐨勮闂檺鍒躲傚鏋滀綘鍏佽鏌愪汉瀵硅嚜宸辩殑鏂伴椈鎶ラ亾杩涜缂栬緫锛屼綘蹇呴』鑳藉纭俊璇ョ敤鎴蜂笉浼氭湭缁忚鍙鍏朵粬浜虹殑鎶ラ亾杩涜缂栬緫銆
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.
鏈鍚庯紝瑕佹敞鎰忕殑鏄鐞嗙晫闈腑缂哄皯鑱氬悎銆備篃灏辨槸璇达紝涓嶆敮鎸佹樉绀烘昏銆佸钩鍧囧间箣绫荤殑涓滆タ銆傚啀娆¢噸鐢筹紝绠$悊鐣岄潰鍙敤浜庣紪杈戔斺斿畠棰勬湡浣犲皢閫氳繃瀹氫箟瑙嗗浘鏉ュ畬鎴愬叾瀹冩墍鏈夊伐浣溿
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/
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爜銆
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涓垪鍑恒
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
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鎵绀虹殑琛ㄥ崟涓鏍风殑涓滆タ銆
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/
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/
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 鎴戝睍绀轰簡鍍忚繖鏍风殑涓涓渶缁堢粨鏋溿
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/
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.
鎸夌収杩欑鏂瑰紡锛屾垜浠彲浠ユ浛鎹㈠垹闄ょ‘璁ら〉銆佺紪杈戦〉闈㈡垨鑰呯鐞嗙晫闈㈢殑鍏跺畠浠讳綍閮ㄥ垎銆
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!
鍓嶈繘锛
鍏充簬鏈瘎娉ㄧ郴缁
鏈珯浣跨敤涓婁笅鏂囧叧鑱旂殑璇勬敞绯荤粺鏉ユ敹闆嗗弽棣堜俊鎭備笉鍚屼簬涓鑸鏁寸珷鍋氳瘎娉ㄧ殑鍋氭硶锛 鎴戜滑鍏佽浣犲姣忎竴涓嫭绔嬬殑鈥滄枃鏈潡鈥濆仛璇勬敞銆備竴涓滄枃鏈潡鈥濈湅璧锋潵鏄繖鏍风殑锛
涓涓滄枃鏈潡鈥濇槸涓涓钀斤紝涓涓垪琛ㄩ」锛屼竴娈典唬鐮侊紝鎴栬呭叾浠栦竴灏忔鍐呭銆 浣犻変腑瀹冧細楂樹寒搴︽樉绀:
瑕佸鏂囨湰鍧楀仛璇勬敞锛屼綘鍙渶瑕佺偣鍑诲畠鏃佽竟鐨勬爣璇嗗潡:
鎴戜滑浼氫粩缁嗛槄璇绘瘡涓瘎璁猴紝濡傛灉鍙兘鐨勮瘽鎴戜滑涔熶細鎶婅瘎娉ㄨ冭檻鍒版湭鏉ョ殑鐗堟湰涓幓:
濡傛灉浣犳効鎰忎綘鐨勮瘎娉ㄨ閲囩敤锛岃纭繚鐣欎笅浣犵殑鍏ㄥ悕 (娉ㄦ剰涓嶆槸鏄电О鎴栫畝绉帮級
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.