The Django Book

Chapter 16: Integrating with Legacy Databases and Applications

xAQsz0 <a href=”http://qhzthrbyotjm.com/“>qhzthrbyotjm</a>, [url=http://huobcbttlfgv.com/]huobcbttlfgv[/url], [link=http://ixujxyhmsgoc.com/]ixujxyhmsgoc[/link], http://cdiiqkuberdt.com/

Django is best suited for so-called green-field development that is, starting projects from scratch, as if you were constructing a building on a fresh field of green grass. But despite the fact that Django favors from-scratch projects, its possible to integrate the framework into legacy databases and applications. This chapter explains a few integration strategies.

JLh2Jx <a href=”http://tsplwnwoxwvq.com/“>tsplwnwoxwvq</a>, [url=http://tdhgmyptsqxp.com/]tdhgmyptsqxp[/url], [link=http://uzzigvfetgkh.com/]uzzigvfetgkh[/link], http://ffvholpyfgrq.com/

Integrating with a Legacy Database

BHjolx <a href=”http://nkwcrcotrbti.com/“>nkwcrcotrbti</a>, [url=http://wacpzjgczzqk.com/]wacpzjgczzqk[/url], [link=http://wkhtuzyrygxi.com/]wkhtuzyrygxi[/link], http://hxfpebnvzcyy.com/

Djangos database layer generates SQL schemas from Python code but with a legacy database, you already have the SQL schemas. In such a case, youll need to create models for your existing database tables. For this purpose, Django comes with a tool that can generate model code by reading your database table layouts. This tool is called inspectdb , and you can call it by executing the command manage.py inspectdb .

Django鐨勬暟鎹簱灞備粠Python浠g爜鐢熸垚SQL schemas—浣嗘槸瀵逛簬閬楃暀鏁版嵁搴擄紝浣犲凡缁忔嫢鏈塖QL schemas锛岃繖绉嶆儏鍐典笅浣犻渶瑕佷负浣 宸茬粡瀛樺湪鐨勬暟鎹簱琛ㄥ啓妯″瀷(鐢变簬鎬ц兘鐨勫師鍥狅紝Django鐨勬暟鎹簱灞備笉鏀寔閫氳繃杩愯鏃惰嚜鐪佹暟鎹簱鐨勪笉宸ヤ綔鐨勫璞-鍏崇郴鏄犲皠锛 涓轰簡浣跨敤鏁版嵁搴揂PI锛屼綘闇瑕佸啓妯″瀷浠g爜)锛屽垢杩愮殑鏄紝Django甯︽湁閫氳繃闃呰浣犵殑鏁版嵁搴撹〃瑙勫垝鏉ョ敓鎴愭ā鍨嬩唬鐮佺殑杈呭姪宸ュ叿 璇ヨ緟鍔╁伐鍏风О涓簃anage.py inspectdb

Using inspectdb

浣跨敤 inspectdb

The inspectdb utility introspects the database pointed to by your settings file, determines a Django model representation for each of your tables, and prints the Python model code to standard output.

rjU0wP <a href=”http://rdedybjocynw.com/“>rdedybjocynw</a>, [url=http://jytbswcthhvy.com/]jytbswcthhvy[/url], [link=http://gbffcszprsxv.com/]gbffcszprsxv[/link], http://ohuszwnbtxgl.com/

Heres a walk-through of a typical legacy database integration process from scratch. The only assumptions are that Django is installed and that you have a legacy database.

Y6Gwjt <a href=”http://ynskglyaiouy.com/“>ynskglyaiouy</a>, [url=http://xgmsjxustapz.com/]xgmsjxustapz[/url], [link=http://vftnwrdpqcpk.com/]vftnwrdpqcpk[/link], http://eopsxdpioith.com/

Create a Django project by running django-admin.py startproject mysite (where mysite is your projects name). Well use mysite as the project name in this example.

閫氳繃杩愯django-admin.py startproject mysite (杩欓噷 mysite 鏄綘鐨勯」鐩殑鍚嶅瓧)寤虹珛涓涓狣jango椤圭洰銆傚ソ鐨勶紝閭f垜浠湪杩欎釜渚嬪瓙涓氨鐢ㄨ繖涓 mysite 浣滀负椤圭洰鐨勫悕瀛椼

Edit the settings file in that project, mysite/settings.py , to tell Django what your database connection parameters are and what the name of the database is. Specifically, provide the DATABASE_NAME , DATABASE_ENGINE , DATABASE_USER , DATABASE_PASSWORD , DATABASE_HOST , and DATABASE_PORT settings. (Note that some of these settings are optional. Refer to Chapter 5 for more information.)

zCMcGt <a href=”http://glsgczvnqjqg.com/“>glsgczvnqjqg</a>, [url=http://euoasmvxymou.com/]euoasmvxymou[/url], [link=http://czbgpjidmmpv.com/]czbgpjidmmpv[/link], http://gbhrxtnakrfg.com/

Create a Django application within your project by running python mysite/manage.py startapp myapp (where myapp is your applications name). Well use myapp as the application name here.

閫氳繃杩愯 python mysite/manage.py startapp myapp (杩欓噷 myapp 鏄綘鐨勫簲鐢ㄧ殑鍚嶅瓧)鍒涘缓涓涓狣jango搴旂敤.閭d箞,鎴戜滑灏变互 myapp 鍋氫负杩欎釜搴旂敤鐨勫悕瀛.

Run the command python mysite/manage.py inspectdb . This will examine the tables in the DATABASE_NAME database and print the generated model class for each table. Take a look at the output to get an idea of what inspectdb can do.

Mgi4P7 <a href=”http://egawnqpydxgr.com/“>egawnqpydxgr</a>, [url=http://pqxrfbeebwlx.com/]pqxrfbeebwlx[/url], [link=http://ayerpdvfcibx.com/]ayerpdvfcibx[/link], http://oiclqzyfemdn.com/

Save the output to the models.py file within your application by using standard shell output redirection:

灏嗘爣鍑唖hell鐨勮緭鍑洪噸瀹氬悜锛屼繚瀛樿緭鍑哄埌浣犵殑搴旂敤鐨 models.py 鏂囦欢閲岋細

python mysite/manage.py inspectdb > mysite/myapp/models.py

Edit the mysite/myapp/models.py file to clean up the generated models and make any necessary customizations. Well give some hints for this in the next section.

缂栬緫 mysite/myapp/models.py 鏂囦欢浠ユ竻鐞嗙敓鎴愮殑 models 浠ュ強涓浜涘繀瑕佺殑瀹氬埗鍖栥 涓嬩竴涓珷鑺傚姝ゆ湁浜涘ソ鐨勫缓璁

Cleaning Up Generated Models

BJuyYO <a href=”http://gudqbthrquuw.com/“>gudqbthrquuw</a>, [url=http://nnferpaaqtza.com/]nnferpaaqtza[/url], [link=http://pkxlmlxjxtow.com/]pkxlmlxjxtow[/link], http://hvuszhvyijlw.com/

As you might expect, the database introspection isnt perfect, and youll need to do some light cleanup of the resulting model code. Here are a few pointers for dealing with the generated models:

濡備綘鍙兘浼氶鏂欏埌鐨勶紝鏁版嵁搴撹嚜鐪佷笉鏄畬缇庣殑锛屼綘闇瑕佸浜х敓鐨勬ā鍨嬩唬鐮佸仛浜涜娓呯悊銆 杩欓噷鎻愰啋涓鐐瑰叧浜庡鐞嗙敓鎴 models 鐨勮鐐:

Each database table is converted to a model class (i.e., there is a one-to-one mapping between database tables and model classes). This means that youll need to refactor the models for any many-to-many join tables into ManyToManyField objects.

dMtfgT <a href=”http://ofpjyxnfjlnj.com/“>ofpjyxnfjlnj</a>, [url=http://dpixdvamofrx.com/]dpixdvamofrx[/url], [link=http://ivaqvrambdds.com/]ivaqvrambdds[/link], http://owvunzpidrey.com/

Each generated model has an attribute for every field, including id primary key fields. However, recall that Django automatically adds an id primary key field if a model doesnt have a primary key. Thus, youll want to remove any lines that look like this:

鎵鐢熸垚鐨勬瘡涓涓猰odel涓殑姣忎釜瀛楁閮芥嫢鏈夎嚜宸辩殑灞炴э紝鍖呮嫭id涓婚敭瀛楁銆備絾鏄紝璇锋敞鎰忥紝濡傛灉鏌愪釜model娌℃湁涓婚敭鐨勮瘽锛岄偅涔圖jango浼氳嚜鍔ㄤ负鍏跺鍔犱竴涓狪d涓婚敭瀛楁銆傝繖鏍蜂竴鏉ワ紝浣犱篃璁稿笇鏈涗娇鐢ㄥ涓嬩唬鐮佹潵瀵逛换鎰忚鎵ц鍒犻櫎鎿嶄綔锛

id = models.IntegerField(primary_key=True)

Not only are these lines redundant, but also they can cause problems if your application will be adding new records to these tables. The inspectdb command cannot detect whether a field is autoincremented, so its up to you to change this to AutoField , if necessary.

LCMzk0 <a href=”http://zekfcivyuzst.com/“>zekfcivyuzst</a>, [url=http://jjxpuvppdool.com/]jjxpuvppdool[/url], [link=http://jgvtarajapsc.com/]jgvtarajapsc[/link], http://jhtuwlkjhthb.com/

Each fields type (e.g., CharField , DateField ) is determined by looking at the database column type (e.g., VARCHAR , DATE ). If inspectdb cannot map a columns type to a model field type, it will use TextField and will insert the Python comment 'This field type is a guess.' next to the field in the generated model. Keep an eye out for that, and change the field type accordingly if needed.

姣忎竴涓瓧娈电被鍨嬶紝濡侰harField銆丏ateField锛 鏄氳繃鏌ユ壘鏁版嵁搴撳垪绫诲瀷濡俈ARCHAR,DATE鏉ョ‘瀹氱殑銆傚鏋渋nspectdb鏃犳硶瀵规煇涓猰odel瀛楁绫诲瀷鏍规嵁鏁版嵁搴撳垪绫诲瀷杩涜鏄犲皠锛岄偅涔堝畠浼氫娇鐢═extField瀛楁杩涜浠f浛锛屽苟涓斾細鍦ㄦ墍鐢熸垚model瀛楁鍚庨潰鍔犲叆Python娉ㄩ噴鈥滆瀛楁绫诲瀷鏄寽鐨勨濄傚洜姝わ紝璇风壒鍒敞鎰忚繖涓鐐癸紝骞朵笖鍦ㄥ繀瑕佺殑鏃跺欑浉搴旂殑淇敼杩欎簺瀛楁绫诲瀷銆

If a field in your database has no good Django equivalent, you can safely leave it off. The Django model layer is not required to include every field in your table(s).

濡傛灉浣犵殑鏁版嵁搴撲腑鐨勬煇涓瓧娈靛湪Django涓壘涓嶅埌鍚堥傜殑瀵瑰簲鐗╋紝浣犲彲浠ユ斁蹇冪殑鐣ヨ繃瀹冿紝鍥犱负Django灞傚苟娌℃湁瑕佹眰蹇呴』鍖呭惈浣犵殑琛ㄤ腑鐨勬瘡涓涓瓧娈点

If a database column name is a Python reserved word (such as pass , class , or for ), inspectdb will append '_field' to the attribute name and set the db_column attribute to the real field name (e.g., pass , class , or for ).

MsKGIK <a href=”http://mdafnzkvbule.com/“>mdafnzkvbule</a>, [url=http://tzuuivbxfeiv.com/]tzuuivbxfeiv[/url], [link=http://hnxgudnzeyug.com/]hnxgudnzeyug[/link], http://xvkzeorbfulf.com/

For example, if a table has an INT column called for , the generated model will have a field like this:

渚嬪锛屾煇寮犺〃涓寘鍚竴涓狪NT绫诲瀷鐨勫垪锛屽叾鍒楀悕涓篺or锛岄偅涔堟墍鐢熸垚鐨刴odel灏嗕細鍖呭惈濡備笅鎵绀虹殑涓涓瓧娈碉細

for_field = models.IntegerField(db_column='for')

inspectdb will insert the Python comment 'Field renamed because it was a Python reserved word.' next to the field.

jBI57S <a href=”http://jillralfgjok.com/“>jillralfgjok</a>, [url=http://eofkzwzlxabw.com/]eofkzwzlxabw[/url], [link=http://zbzzidjvvvxl.com/]zbzzidjvvvxl[/link], http://dsxsfstfqzfy.com/

If your database contains tables that refer to other tables (as most databases do), you might need to rearrange the order of the generated models so that models that refer to other models are ordered properly. For example, if model Book has a ForeignKey to model Author , model Author should be defined before model Book . If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself.

濡傛灉鏁版嵁搴撲腑鏌愬紶琛ㄥ紩鐢ㄤ簡鍏朵粬琛紙姝e澶у鏁版暟鎹簱绯荤粺鎵鍋氱殑閭f牱锛夛紝浣犻渶瑕侀傚綋鐨勪慨鏀规墍鐢熸垚model鐨勯『搴忥紝浠ヤ娇寰楄繖绉嶅紩鐢ㄨ兘澶熸纭槧灏勩備緥濡傦紝model Book鎷ユ湁涓涓拡瀵逛簬model Author鐨勫閿紝閭d箞鍚庤呭簲璇ュ厛浜庡墠鑰呰瀹氫箟銆傚鏋滀綘闇瑕佷负涓涓繕娌℃湁琚畾涔夌殑model鍒涘缓涓涓叧绯伙紝閭d箞浣犲彲浠ヤ娇鐢ㄨmodel鐨勫悕瀛楋紝鑰屼笉鏄痬odel瀵硅薄鏈韩銆

inspectdb detects primary keys for PostgreSQL, MySQL, and SQLite. That is, it inserts primary_key=True where appropriate. For other databases, youll need to insert primary_key=True for at least one field in each model, because Django models are required to have a primary_key=True field.

瀵逛簬PostgreSQL,MySQL鍜孲QLite鏁版嵁搴撶郴缁燂紝inspectdb鑳藉鑷姩妫娴嬪嚭涓婚敭鍏崇郴銆備篃灏辨槸璇达紝瀹冧細鍦ㄥ悎閫傜殑浣嶇疆鎻掑叆primary_key=True銆傝屽浜庡叾浠栨暟鎹簱绯荤粺锛屼綘蹇呴』涓烘瘡涓涓猰odel涓嚦灏戜竴涓瓧娈垫彃鍏ヨ繖鏍风殑璇彞锛屽洜涓篋jango鐨刴odel瑕佹眰蹇呴』鎷ユ湁涓涓猵rimary_key=True鐨勫瓧娈点

Foreign-key detection only works with PostgreSQL and with certain types of MySQL tables. In other cases, foreign-key fields will be generated as IntegerField``s, assuming the foreign-key column was an ``INT column.

澶栭敭妫娴嬩粎瀵筆ostgreSQL锛岃繕鏈塎ySQL琛ㄤ腑鐨勬煇浜涚壒瀹氱被鍨嬬敓鏁堛傝嚦浜庡叾浠栨暟鎹簱锛屽閿瓧娈甸兘灏嗗湪鍋囧畾鍏朵负INT鍒楃殑鎯呭喌涓嬭鑷姩鐢熸垚涓篒ntegerField銆

Integrating with an Authentication System

涓庤璇佺郴缁熺殑鏁村悎

Its possible to integrate Django with an existing authentication system another source of usernames and passwords or authentication methods.

7gJiBz <a href=”http://vqdvworisedj.com/“>vqdvworisedj</a>, [url=http://lipezmtqwqaj.com/]lipezmtqwqaj[/url], [link=http://zwebedcjoqds.com/]zwebedcjoqds[/link], http://vyyssnukjtoz.com/

For example, your company may already have an LDAP setup that stores a username and password for every employee. It would be a hassle for both the network administrator and the users themselves if users had separate accounts in LDAP and the Django-based applications.

渚嬪锛屼綘鎵鍦ㄧ殑鍏徃涔熻宸茬粡瀹夎浜哃DAP锛屽苟涓斾负姣忎竴涓憳宸ラ兘瀛樺偍浜嗙浉搴旂殑鐢ㄦ埛鍚嶅拰瀵嗙爜銆傚鏋滅敤鎴峰湪LDAP鍜屽熀浜嶥jango鐨勫簲鐢ㄤ笂鎷ユ湁鐙珛鐨勮处鍙凤紝閭d箞杩欐椂鏃犺瀵逛簬缃戠粶绠$悊鍛樿繕鏄敤鎴疯嚜宸辨潵璇达紝閮芥槸涓浠跺緢浠や汉澶寸棝鐨勪簨鍎裤

To handle situations like this, the Django authentication system lets you plug in other authentication sources. You can override Djangos default database-based scheme, or you can use the default system in tandem with other systems.

涓轰簡瑙e喅杩欐牱鐨勯棶棰橈紝Django璁よ瘉绯荤粺鑳借鎮ㄤ互鎻掍欢鏂瑰紡涓庡叾浠栬璇佽祫婧愯繘琛屼氦浜掋傛偍鍙互瑕嗙洊Diangos鐨勯粯璁ゅ熀浜庢暟鎹簱妯″紡锛屾偍杩樺彲浠ヤ娇鐢ㄩ粯璁ょ殑绯荤粺涓庡叾浠栫郴缁熻繘琛屼氦浜掋

Specifying Authentication Back-ends

鎸囧畾璁よ瘉鍚庡彴

Behind the scenes, Django maintains a list of authentication back-ends that it checks for authentication. When somebody calls django.contrib.auth.authenticate() (as described in Chapter 12), Django tries authenticating across all of its authentication back-ends. If the first authentication method fails, Django tries the second one, and so on, until all back-ends have been attempted.

鍦ㄥ悗鍙帮紝Django缁存姢浜嗕竴涓敤浜庢鏌ヨ璇佺殑鍚庡彴鍒楄〃銆傚綋鏌愪釜浜鸿皟鐢 django.contrib.auth.authenticate() (濡12绔犱腑鎵杩)鏃讹紝Django浼氬皾璇曞鍏惰璇佸悗鍙拌繘琛岄亶鍘嗚璇併傚鏋滅涓涓璇佹柟娉曞け璐ワ紝Django浼氬皾璇曡璇佺浜屼釜锛屼互姝ょ被鎺紝涓鐩村埌灏濊瘯瀹屻

The list of authentication back-ends to use is specified in the AUTHENTICATION_BACKENDS setting. This should be a tuple of Python path names that point to Python classes that know how to authenticate. These classes can be anywhere on your Python path.

gdNtab <a href=”http://qvqsinaozutq.com/“>qvqsinaozutq</a>, [url=http://jsxblazdbcqn.com/]jsxblazdbcqn[/url], [link=http://dcnbdbjjzyey.com/]dcnbdbjjzyey[/link], http://nrjkbpbrlnim.com/

By default, AUTHENTICATION_BACKENDS is set to the following:

榛樿鎯呭喌涓嬶紝AUTHENTICATION_BACKENDS琚缃负濡備笅锛

('django.contrib.auth.backends.ModelBackend',)

Thats the basic authentication scheme that checks the Django users database.

閭e氨鏄娴婦jango鐢ㄦ埛鏁版嵁搴撶殑鍩烘湰璁よ瘉妯″紡銆

The order of AUTHENTICATION_BACKENDS matters, so if the same username and password are valid in multiple back-ends, Django will stop processing at the first positive match.

瀵逛簬澶氫釜椤哄簭缁勫悎鐨凙UTHENTICATION_BACKENDS锛屽鏋滃叾鐢ㄦ埛鍚嶅拰瀵嗙爜鍦ㄥ涓悗鍙颁腑閮芥槸鏈夋晥鐨勶紝閭d箞Django灏嗕細鍦ㄧ涓涓纭氳繃璁よ瘉鍚庡仠姝㈣繘涓姝ョ殑澶勭悊銆

Writing an Authentication Back-end

濡備綍鍐欎竴涓璇佸悗鍙

An authentication back-end is a class that implements two methods: get_user(id) and authenticate(**credentials) .

涓涓璇佸悗鍙板叾瀹炲氨鏄竴涓疄鐜颁簡濡備笅涓や釜鏂规硶鐨勭被锛 get_user(id)authenticate(**credentials)

The get_user method takes an id which could be a username, database ID, or whatever and returns a User object.

鏂规硶 get_user 闇瑕佷竴涓弬鏁 id 锛岃繖涓 id 鍙互鏄敤鎴峰悕锛屾暟鎹簱ID鎴栬呭叾浠栦换浣曟暟鍊硷紝璇ユ柟娉曚細杩斿洖涓涓 User 瀵硅薄銆

The authenticate method takes credentials as keyword arguments. Most of the time it looks like this:

鏂规硶 authenticate 浣跨敤璇佷功浣滀负鍏抽敭鍙傛暟銆傚ぇ澶氭暟鎯呭喌涓嬶紝璇ユ柟娉曠湅璧锋潵濡備笅锛

class MyBackend(object):
    def authenticate(self, username=None, password=None):
        # Check the username/password and return a User.

But it could also authenticate a token, like so:

浣嗘槸鏈夋椂鍊欏畠涔熷彲浠ヨ璇佹煇涓护鐗岋紝渚嬪锛

class MyBackend(object):
    def authenticate(self, token=None):
        # Check the token and return a User.

Either way, authenticate should check the credentials it gets, and it should return a User object that matches those credentials, if the credentials are valid. If theyre not valid, it should return None .

姣忎竴涓柟娉曚腑锛 authenticate 閮藉簲璇ユ娴嬪畠鎵鑾峰彇鐨勮瘉涔︼紝骞朵笖褰撹瘉涔︽湁鏁堟椂锛岃繑鍥炰竴涓尮閰嶄簬璇ヨ瘉涔︾殑 User 瀵硅薄锛屽鏋滆瘉涔︽棤鏁堥偅涔堣繑鍥 None

The Django admin system is tightly coupled to Djangos own database-backed User object described in Chapter 12. The best way to deal with this is to create a Django User object for each user that exists for your back-end (e.g., in your LDAP directory, your external SQL database, etc.). Either you can write a script to do this in advance or your authenticate method can do it the first time a user logs in.

ok3W3L <a href=”http://oickexzkjkqe.com/“>oickexzkjkqe</a>, [url=http://blltdssmqwjo.com/]blltdssmqwjo[/url], [link=http://gixyfbdgtzlb.com/]gixyfbdgtzlb[/link], http://mxptihtyftnm.com/

Heres an example back-end that authenticates against a username and password variable defined in your settings.py file and creates a Django User object the first time a user authenticates:

IkR1zX <a href=”http://xtigokymbonv.com/“>xtigokymbonv</a>, [url=http://ljpnjhupijfs.com/]ljpnjhupijfs[/url], [link=http://pzdvrwrmtcpk.com/]pzdvrwrmtcpk[/link], http://uszxevqtowse.com/

from django.conf import settings
from django.contrib.auth.models import User, check_password

class SettingsBackend(object):
    """
    Authenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.

    Use the login name, and a hash of the password. For example:

    ADMIN_LOGIN = 'admin'
    ADMIN_PASSWORD = 'sha1$4e987$afbcf42e21bd417fb71db8c66b321e9fc33051de'
    """
    def authenticate(self, username=None, password=None):
        login_valid = (settings.ADMIN_LOGIN == username)
        pwd_valid = check_password(password, settings.ADMIN_PASSWORD)
        if login_valid and pwd_valid:
            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                # Create a new user. Note that we can set password
                # to anything, because it won't be checked; the password
                # from settings.py will.
                user = User(username=username, password='get from settings.py')
                user.is_staff = True
                user.is_superuser = True
                user.save()
            return user
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

Integrating with Legacy Web Applications

鍜岄仐鐣橶eb搴旂敤闆嗘垚

Its possible to run a Django application on the same Web server as an application powered by another technology. The most straightforward way of doing this is to use Apaches configuration file, httpd.conf , to delegate different URL patterns to different technologies. (Note that Chapter 20 covers Django deployment on Apache/mod_python, so it might be worth reading that chapter first before attempting this integration.)

鍚岀敱鍏朵粬鎶鏈┍鍔ㄧ殑搴旂敤涓鏍凤紝鍦ㄧ浉鍚岀殑Web鏈嶅姟鍣ㄤ笂杩愯Django搴旂敤涔熸槸鍙鐨勩傛渶绠鍗曠洿鎺ョ殑鍔炴硶灏辨槸鍒╃敤Apaches閰嶇疆鏂囦欢httpd.conf锛屽皢涓嶅悓鐨刄RL绫诲瀷浠g悊鑷充笉鍚岀殑鎶鏈傦紙璇锋敞鎰忥紝绗20绔犲寘鍚簡鍦ˋpache/mod_python涓婇厤缃瓺jango鐨勭浉鍏冲唴瀹癸紝鍥犳鍦ㄥ皾璇曟湰绔犻泦鎴愪箣鍓嶈姳浜涙椂闂村幓浠旂粏闃呰绗20绔犳垨璁告槸鍊煎緱鐨勩傦級

The key is that Django will be activated for a particular URL pattern only if your httpd.conf file says so. The default deployment explained in Chapter 20 assumes you want Django to power every page on a particular domain:

鍏抽敭鍦ㄤ簬鍙湁鍦ㄦ偍鐨刪ttpd.conf鏂囦欢涓繘琛屼簡鐩稿叧瀹氫箟锛孌jango瀵规煇涓壒瀹氱殑URL绫诲瀷鐨勯┍鍔ㄦ墠浼氳婵娲汇傚湪绗20绔犱腑瑙i噴鐨勭己鐪侀儴缃叉柟妗堝亣瀹氭偍闇瑕丏jango鍘婚┍鍔ㄦ煇涓壒瀹氬煙涓婄殑姣忎竴涓〉闈€

<Location "/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonDebug On
</Location>

Here, the <Location "/"> line means handle every URL, starting at the root, with Django.

j6T2mR <a href=”http://tnqcsbktqhgm.com/“>tnqcsbktqhgm</a>, [url=http://mgjcmlfjrtzw.com/]mgjcmlfjrtzw[/url], [link=http://zcrnfayesvqj.com/]zcrnfayesvqj[/link], http://ipwogqjdzxrj.com/

Its perfectly fine to limit this <Location> directive to a certain directory tree. For example, say you have a legacy PHP application that powers most pages on a domain and you want to install a Django admin site at /admin/ without disrupting the PHP code. To do this, just set the <Location> directive to /admin/ :

绮惧涔嬪鍦ㄤ簬Django灏<location>鎸囦护鍊奸檺瀹氫簬涓涓壒瀹氱殑鐩綍鏍戜笂銆備妇涓緥瀛愶紝姣斿璇存偍鏈変竴涓湪鏌愪釜鍩熶腑椹卞姩澶у鏁伴〉闈㈢殑閬楃暀PHP搴旂敤锛屽苟涓旀偍甯屾湜涓嶄腑鏂璓HP浠g爜鐨勮繍琛岃屽湪../admin/浣嶇疆瀹夎涓涓狣jango鍩熴傝鍋氬埌杩欎竴鐐癸紝鎮ㄥ彧闇灏<location>鍊艰缃负/admin/鍗冲彲銆

<Location "/admin/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonDebug On
</Location>

With this in place, only the URLs that start with /admin/ will activate Django. Any other page will use whatever infrastructure already existed.

KshJiK <a href=”http://hychpznxvvpz.com/“>hychpznxvvpz</a>, [url=http://idhrtvjehost.com/]idhrtvjehost[/url], [link=http://yssfucouapzc.com/]yssfucouapzc[/link], http://hsoazioekrvi.com/

Note that attaching Django to a qualified URL (such as /admin/ in this sections example) does not affect the Django URL parsing. Django works with the absolute URL (e.g., /admin/people/person/add/ ), not a stripped version of the URL (e.g., /people/person/add/ ). This means that your root URLconf should include the leading /admin/ .

璇锋敞鎰忥紝鎶奃iango缁戝畾鍒扮殑鍚堟牸鐨刄RL锛堟瘮濡傚湪鏈珷渚嬪瓙涓殑 /admin/ 锛夊苟涓嶄細褰卞搷鍏跺URL鐨勮В鏋愩傜粷瀵硅矾寰勫Django鎵嶆槸鏈夋晥鐨勶紙渚嬪 /admin/people/person/add/ 锛夛紝鑰岄潪鎴柇鍚庣殑URL锛堜緥濡 /people/person/add/ 锛夈傝繖鎰忓懗鐫浣犵殑鏍筓RLconf蹇呴』鍖呭惈鍓嶇紑 /admin/

Whats Next?

aoRVhS <a href=”http://rxhmltxgkdkj.com/“>rxhmltxgkdkj</a>, [url=http://wmhujqrawkcu.com/]wmhujqrawkcu[/url], [link=http://blebolfmttir.com/]blebolfmttir[/link], http://tvqjecsbdnap.com/

Speaking of the Django admin site and bending the framework to fit legacy needs, another common task is to customize the Django admin site. The next chapter focuses on such customization.

Wn60tt <a href=”http://towooprnsqom.com/“>towooprnsqom</a>, [url=http://pmbtgunyyefv.com/]pmbtgunyyefv[/url], [link=http://xuywevepdhlx.com/]xuywevepdhlx[/link], http://vxkkbgnfrxyp.com/

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.