django-admin.py is Djangos command-line utility for administrative tasks. This appendix explains its many powers.
django-admin.py 鏄疍jango绠$悊浠诲姟鐨勫懡浠よ宸ュ叿銆傛湰闄勫綍绠杩板畠鐨勫涓壒鎬с
Youll usually access django-admin.py through a projects manage.py wrapper. manage.py is automatically created in each Django project and is a thin wrapper around django-admin.py . It takes care of two things for you before delegating to django-admin.py :
LNVKgB <a href=”http://vbksqrondtwn.com/“>vbksqrondtwn</a>, [url=http://bmvlidwwfxwa.com/]bmvlidwwfxwa[/url], [link=http://hpsplsneovpa.com/]hpsplsneovpa[/link], http://txkxvkmiotqh.com/
It puts your projects package on sys.path .
灏嗕綘椤圭洰鎵鍦ㄨ矾寰勫姞鍏 sys.path .
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your projects settings.py file.
璁剧疆鐜鍙橀噺 DJANGO_SETTINGS_MODULE , 浣垮叾鎸囧悜浣犻」鐩腑鐨 settings.py
The django-admin.py script should be on your system path if you installed Django via its setup.py utility. If its not on your path, you can find it in site-packages/django/bin within your Python installation. Consider symlinking it from some place on your path, such as /usr/local/bin .
濡傛灉浣犳槸浣跨敤 setup.py 鏉ュ畨瑁匘jango鐨勶紝閭d箞 django-admin.py 搴旇宸茬粡鍦ㄤ綘鐨勭郴缁熻矾寰勪腑浜. 濡傛灉娌℃湁锛屼綘鍙互鍦ㄤ綘python瀹夎璺緞涓嬬殑 site-packages/django/bin 涓壘鍒板畠. 鍙互鍦ㄦ煇涓郴缁熻矾寰勪笂寤虹珛涓涓寚鍚戣鏂囦欢鐨勭鍙烽摼鎺ワ紝濡傚湪 /usr/local/bin
Windows users, who do not have symlinking functionality available, can copy django-admin.py to a location on their existing path or edit the PATH settings (under Settings ~TRA Control Panel ~TRA System ~TRA Advanced ~TRA Environment) to point to its installed location.
瀵逛簬涓嶈兘寤虹珛绗﹀彿閾炬帴鐨刉indows鐢ㄦ埛, 鍙互鎶 django-admin.py 鎷疯礉鍒扮幆澧冨彉閲 PATH 瀵瑰簲鐨勬煇涓洰褰曚腑锛屾垨鑰呯紪杈戠幆澧冨彉閲 PATH (鍦ㄦ帶鍒堕潰鏉->绯荤粺->楂樼骇->鐜鍙橀噺), 娣诲姞 django-admin.py 鎵鍦ㄨ矾寰.
Generally, when working on a single Django project, its easier to use manage.py . Use django-admin.py with DJANGO_SETTINGS_MODULE or the --settings command-line option, if you need to switch between multiple Django settings files.
涓鑸潵璇达紝鍦ㄥ崟涓鐨凞jango椤圭洰涓紝浣跨敤 manage.py 姣旇緝鏂逛究銆傚鏋滈渶瑕佸湪澶氫釜Django 椤圭洰锛坰etting files锛変腑鍒囨崲锛屽彲浠ヤ娇鐢 django-admin.py 锛 缁撳悎鐜鍙橀噺 DJANGO_SETTINGS_MODULE 鎴栭夐」 --settings
The command-line examples throughout this appendix use django-admin.py to be consistent, but any example can use manage.py just as well.
涓轰繚鎸佷竴鑷达紝鍦ㄦ湰闄勫綍涓殑渚嬪瓙涓潎浣跨敤 django-admin.py , 鎵鏈変緥瀛愰兘鍙互鎹㈢敤 manage.py
The basic usage is:
鍩烘湰鐢ㄦ硶濡備笅锛
django-admin.py action [options]
or:
鎴栬咃細
manage.py action [options]
action should be one of the actions listed in this document. options , which is optional, should be zero or more of the options listed in this document.
鍏朵腑 action 鏄◢鍚庣粰鍑虹殑 action 鍒楄〃涓殑涓涓紝 options 涓哄彲閫夐」锛屽彲浠ョ暀绌烘垨鑰呮槸绋嶅悗缁欏嚭鐨 options 鍒楄〃涓殑涓涓
Run django-admin.py --help to display a help message that includes a terse list of all available actions and options.
杩愯 django-admin.py --help 鍙互鐪嬪埌甯姪淇℃伅锛屽叾涓甫鏈夌簿绠鐨 action 鍜 option 鍒楄〃
Most actions take a list of app names. An app name is the base name of the package containing your models. For example, if your INSTALLED_APPS contains the string 'mysite.blog' , the app name is blog .
澶氭暟 action 鎺ュ彈 app name 鐨勫垪琛ㄤ负鍙傛暟锛 app name 鏄寚鍖呭惈浣犲悇涓ā鍧楃殑鍖呯殑 base name銆 姣旀柟璇达紝濡傛灉浣犵殑鐜鍙橀噺 INSTALLED_APPS 涓惈鏈 'mysite.blog' 锛 閭d箞app name灏辨槸 blog .
The following sections cover the actions available to you.
浠ヤ笅鍒楀嚭浜嗗彲浠ヤ娇鐢ㄧ殑action
adminindex [appname appname ]
Prints the admin-index template snippet for the given application names. Use admin-index template snippets if you want to customize the look and feel of your admins index page.
鏄剧ず鎸囧畾绋嬪簭鐨刟dmin-index妯$増浠g爜銆傚鏋滀綘鎯冲畾鍒惰嚜宸查鏍肩殑admin棣栭〉锛屽彲浠ヤ娇鐢╝dmin-index妯℃澘浠g爜銆
createcachetable [tablename] 鏂板蹇彇璩囨枡琛╗琛ㄧ殑鍚嶇ū]
Creates a cache table named tablename for use with the database cache back-end. See Chapter 13 for more about caching.
涓烘暟鎹簱缂撳瓨鍚庡彴鍒涘缓鍚嶄负 tablename 鐨勭紦瀛樿〃锛岃瑙佺13绔犲叧浜庣紦瀛樼殑閮ㄥ垎
FLfYG4 <a href=”http://vaezfkqrgtwi.com/“>vaezfkqrgtwi</a>, [url=http://akxjprqekcvy.com/]akxjprqekcvy[/url], [link=http://mygpmoogexng.com/]mygpmoogexng[/link], http://kdkspxlzpjin.com/
Runs the command-line client for the database engine specified in your DATABASE_ENGINE setting, with the connection parameters specified in the settings DATABASE_USER , DATABASE_PASSWORD , and so forth.
涓轰綘鍦╜DATABASE_ENGINE` 涓畾涔夌殑鏁版嵁搴撹繍琛屼竴涓懡浠よ瀹㈡埛绔傝繛鎺ユ墍闇瑕佺殑鍙傛暟瀹氫箟鍦╜DATABASE_USER`` , ``DATABASE_PASSWORD``涓傚涓嬨
For PostgreSQL, this runs the psql command-line client.
瀵逛簬 PostgreSQL, 瀹冭繍琛宍`psql`` 鍛戒护
For MySQL, this runs the mysql command-line client.
瀵逛簬 MySQL, 瀹冭繍琛宍`mysql`` 鍛戒护
For SQLite, this runs the sqlite3 command-line client.
瀵逛簬SQLite, 瀹冭繍琛宍`sqlite3`` 鍛戒护
This command assumes the programs are on your PATH so that a simple call to the program name (psql , mysql , or sqlite3 ) will find the program in the right place. Theres no way to specify the location of the program manually.
杩欎釜鍛戒护鍋囪杩欎簺绋嬪簭閮藉湪浣犵殑``PATH``涓紝鎵浠ュ彧瑕佺畝鍗曠殑璋冪敤绋嬪簭鍚(psql , mysql , 鎴 sqlite3 )灏卞彲浠ユ壘鍒板畠浠傛棤娉曟墜宸ュ畾涔夎繖浜涚▼搴忕殑浣嶇疆銆
Displays differences between the current settings file and Djangos default settings.
鏄剧ず褰撳墠settings鏂囦欢鍜孌jango鐨勬爣鍑唖ettings鏂囦欢鐨勪笉鍚屻
Settings that dont appear in the defaults are followed by "###" . For example, the default settings dont define ROOT_URLCONF , so ROOT_URLCONF is followed by "###" in the output of diffsettings .
娌℃湁鍦ㄦ爣鍑唖ettings鏂囦欢涓嚭鐜扮殑璁剧疆鍚庨潰浼氳窡鐫``”###”`銆備緥濡傦紝鏍囧噯settings娌℃湁瀹氫箟``ROOT_URLCONF``锛屽洜姝わ紝鍦╜`diffsettings``鐨勮緭鍑轰腑``ROOT_URLCONF``涔嬪悗浼氳窡鐫“###”```銆
Note that Djangos default settings live in django.conf.global_settings , if youre ever curious to see the full list of defaults.
RhfDXL <a href=”http://nwekewqwmpmf.com/“>nwekewqwmpmf</a>, [url=http://urrjvbmxkjzs.com/]urrjvbmxkjzs[/url], [link=http://ukjmwtvftncx.com/]ukjmwtvftncx[/link], http://zizkdnkhaixj.com/
dumpdata [appname appname ] 灏囨煇鍊嬫噳鐢ㄧ▼寮忕殑璩囨枡涓嬭級鍥炰締
Outputs to standard output all data in the database associated with the named application(s).
杓稿嚭妾旀鍒版婧栬几鍑恒傞棞鏂奸欏嬫噳鐢ㄧ▼寮忚硣鏂欏韩瑁忕殑鎵鏈夎硣鏂欍
By default, the database will be dumped in JSON format. If you want the output to be in another format, use the --format option (e.g., format=xml ). You may specify any Django serialization back-end (including any user-specified serialization back-ends named in the SERIALIZATION_MODULES setting). The --indent option can be used to pretty-print the output.
榛樿鐨勶紝鏁版嵁搴撲細琚鍑轰负JSON鏍煎紡銆傚鏋滀綘瀵煎嚭鍏跺畠鏍煎紡锛屼娇鐢╜`—format``閫夐」( 渚嬪锛宍`format=xml`` )銆備綘鍙互鎸囧畾浠讳綍Django搴忎緥鍚庣(鍖呮嫭浠讳綍鐢ㄦ埛鑷畾涔夌殑搴忎緥鍚庣 锛屽畾涔夊湪``SERIALIZATION_MODULES``涓 ). --indent 閫夐」锛岀敤鏉ユ洿浼樼編鐨勬樉绀鸿緭鍑.
If no application name is provided, all installed applications will be dumped.
濡傛灉娌℃湁鎸囧畾绋嬪簭鍚嶏紝閭d箞鎵鏈夊畨瑁呯殑绋嬪簭閮戒細琚鍑
The output of dumpdata can be used as input for loaddata .
``dumpdata``鐨勮緭鍑烘枃浠讹紝鍙互鐢ㄥ仛``loaddata``鐨勮緭鍏ユ枃浠躲
鍒锋柊
Returns the database to the state it was in immediately after syncdb was executed. This means that all data will be removed from the database, any postsynchronization handlers will be re-executed, and the initial_data fixture will be reinstalled.
璁撹硣鏂欏韩鍥炲埌涓鍊嬪垵濮嬬媭娉侊紝绱勬槸鍦╯yncdb 琚煼琛屼箣寰屻傞欒〃绀烘墍鏈夎硣鏂欏皣寰炶硣鏂欒琚Щ闄わ紝浠讳綍postsynchronization 铏曠悊灏囨渻琚啀铏曠悊銆傜劧寰屽垵濮嬭硣鏂欏皣鏈冨啀琚鍏ヤ竴娆°
妫鏌ユ暟鎹簱
Introspects the database tables in the database pointed to by the DATABASE_NAME setting and outputs a Django model module (a models.py file) to standard output.
灏囨墍鏈夌殑璩囨枡琛ㄦ寚鍑轰締锛屾寜浣犳寚鍚嶇殑璩囨枡搴悕绋辫ō瀹氾紝杓稿嚭涓鍊婦jango 妯″瀷鐨勬ā绲勩(a ``models.py``妾) 鍒版婧栫殑杓稿嚭銆
Use this if you have a legacy database with which youd like to use Django. The script will inspect the database and create a model for each table within it.
鐢ㄥ畠銆傚亣濡備綘宸茬稉鏈変竴鍊嬭佸瓧铏熺殑璩囨枡搴湪鐢紝涓斾綘鎯崇敤Django.閫欏嬫寚浠ゆ獢灏囧伒娓欏嬭硣鏂欏韩锛岀劧寰岀偤姣忎竴鍊嬭硣鏂欒〃鏂板缓涓鍊嬫ā鍨
As you might expect, the created models will have an attribute for every field in the table. Note that inspectdb has a few special cases in its field name output:
浣犲彲鑳芥湡寰咃紝閫欏嬫柊寤虹殑妯″瀷灏囨湁琛ㄨ鎵鏈夋瑒浣嶆墍灏嶆噳鐨勫爆鎬с傛敞鎰:inspectdb鏈変竴浜涚壒鍒ョ殑鐙娉佸湪娆勪綅鍚嶇ū鐨勮几鍑轰笂銆
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.
鍋囧inspectdb涓嶈兘妯欏畾琛岀殑鍨嬪垾鍜屾ā鍨嬭娆勪綅鐨勫瀷鍒ワ紝瀹冨皣鐢═extField锛岀劧寰屽鍏ヤ竴鍊婸ython 瑷昏В锛岄欐瑒浣嶅瀷鍒ユ垜鏄敤鐚滅殑銆傚湪閫欑敓鎴愮殑妯″瀷鐨勬瑒浣嶆梺閭
If the database column name is a Python reserved word (such as 'pass' , 'class' , or 'for' ), inspectdb will append '_field' to the attribute name. For example, if a table has a column 'for' , the generated model will have a field 'for_field' , with the db_column attribute set to 'for' . inspectdb will insert the Python comment 'Field renamed because it was a Python reserved word.' next to the field.
鍋囧璩囨枡搴殑娆勪綅鍚嶇ū鏄疨ython鐨勪繚鐣欏瓧(濡 pass锛宑lass锛宖or)锛岄偅inspectdb鏈冨姞涓奯field鍒伴欏嬫ц唱鍚嶇ū鐨勫緦闈€備緥濡傦紝鍋囧涓鍊嬭〃鏈変竴鍊嬫瑒浣嶅彨鍋歠or锛岄欑敘鐢熺殑妯″瀷灏辨渻鏈変竴鍊嬫瑒浣嶅彨鍋歠or_field锛屽皪鎳夊埌db_column鐨勬ц唱鏄痜or锛宨nspectdb灏囨渻濉叆Python瑷昏В锛屾瑒浣嶈鏀瑰悕瀛椾簡锛屽洜鐐洪欐槸Python鐨勪繚鐣欏瓧銆傚姞涓婃梺閭
This feature is meant as a shortcut, not as definitive model generation. After you run it, youll want to look over the generated models yourself to make customizations. In particular, youll need to rearrange the models so that models with relationships are ordered properly.
閫欓爡鐗硅唱鏄〃绀烘嵎寰戯紝涓嶆槸鏈夊彈闄愮殑妯″瀷鐢㈢敓锛屽湪浣犲煼琛屽畬寰岋紝浣犲皣鎯宠闂滃績涓涓嬮欒嚜鍕曠敘鐢熺殑妯″瀷渚嗗瑁藉寲銆傜壒鍒ユ槸锛屼綘灏囬渶瑕佸緸鏂板畨鎺掗欎簺妯″瀷锛屽缓绔嬩箣闁撶殑闂滀總锛屽拰鎭扮暥鐨勬帓搴
Primary keys are automatically introspected for PostgreSQL, MySQL, and SQLite, in which case Django puts in the primary_key=True where needed.
涓婚嵉鍊兼槸鑷嫊鍋垫脯鐨勶紝灏峆ostgreSQL, MySQL, and SQLite,鍦ㄩ欑ó鎯呮硜涓嬶紝Django 鏈冭闇瑕佸皣閫欎簺瑷畾瑷偤primary_key=True銆
inspectdb works with PostgreSQL, MySQL, and SQLite. Foreign key detection only works in PostgreSQL and with certain types of MySQL tables.
inspectdb锛屽湪閫d笂PostgreSQL, MySQL, and SQLite鏅傦紝澶栭嵉鍋垫脯鍙仛鐢ㄥ湪PostgreSQL 鍙奙ySQL 璩囨枡琛ㄨ鐨勬煇浜涚壒瀹氬瀷鎱!
杓夊叆鏁告摎銆愬~瑁 濉厖銆
Searches for and loads the contents of the named fixture into the database.
灏嬫壘鍜岃級鍏ヤ竴浜涘凡鍛藉悕鐨勫~鍏呯殑鍏у鍒拌硣鏂欏韩瑁
A fixture is a collection of files that contain the serialized contents of the database. Each fixture has a unique name; however, the files that comprise the fixture can be distributed over multiple directories, in multiple applications.
涓鍊嬪~瑁滐紝鏄竴鍊嬫獢妗堢殑鏀堕泦锛屽寘鍚憲璩囨枡搴鐨勯g簩璩囨枡銆傛瘡涓鍊嬪~瑁滐紝鏈変竴鍊嬪敮涓鐨勫悕绋憋紝鐒惰岋紝濉瑁忕殑妾斿彲浠ユ斁鍦ㄥ鍊嬫獢妗堝ぞ瑁忥紝绲﹀鍊嬫噳鐢ㄧ▼寮忚璩囨枡鐢ㄣ
Django will search in three locations for fixtures:
Django 鏈冩壘闂滄柤濉鐨勪笁鍊嬪湴鏂:
In the fixtures directory of every installed application
鍦ㄦ瘡涓鍊嬪凡瀹夎鐨勭▼寮忚鐨刦ixtures鐩寗
In any directory named in the FIXTURE_DIRS setting
鍦‵IXTURE_DIRS瑷畾涓嬬殑锛屼换浣曠洰閷
In the literal path named by the fixture
鍦ㄥ瓧闈笂鐨勮矾寰戞槸鍙栧悕鐐篺ixture鐨
Django will load any and all fixtures it finds in these locations that match the provided fixture names.
Django 灏囪級鍏ユ墍鏈夌殑濉锛屽湪閫欎簺浣嶇疆瑁忚鐧肩従鍒帮紝涓旂鍚堟墍鎻愪緵鐨勫~瑁滅殑鍚嶅瓧銆
If the named fixture has a file extension, only fixtures of that type will be loaded. For example, the following:
鍋囧 宸插懡鍚嶇殑濉鏈変竴鍊嬪壇妾斿悕锛屽彧鏈夋牸寮忓皪鐨勫~瑁滄渻琚級鍏ャ備緥濡備笅鍒
django-admin.py loaddata mydata.json
will only load JSON fixtures called mydata . The fixture extension must correspond to the registered name of a serializer (e.g., json or xml ).
灏囧彧鏈冭級鍏SON 濉鍙仛mydata銆傞欏嬪~瑁滅殑寤朵几绗﹀悎閫欏簭鍒楀瓙鐧婚寗鐨勫悕绋(渚嬪json鎴杧ml)
If you omit the extension, Django will search all available fixture types for a matching fixture. For example, the following:
鍋囧浣犵渷鐣ラ欏嬪壇妾斿悕锛孌jango 灏囧皨鎵炬墍鏈夌殑鍙兘鐨勫~瑁滃瀷鎱嬶紝灏嶆柤涓鍊嬬鍚堢殑濉銆備緥濡傦紝涓嬪垪
django-admin.py loaddata mydata
will look for any fixture of any fixture type called mydata . If a fixture directory contained mydata.json , that fixture would be loaded as a JSON fixture. However, if two fixtures with the same name but different fixture types are discovered (e.g., if mydata.json and mydata.xml were found in the same fixture directory), fixture installation will be aborted, and any data installed in the call to loaddata will be removed from the database.
灏囨渻灏嬫壘浠讳綍濉鍛藉悕鐐簃ydata銆傚亣濡備竴鍊嬪~瑁滅殑鐩寗澶炬湁涓鍊媘ydata.json銆傞欏嬪~瑁滃皣琚級鍏ョ敤锛 JSON鐨勬柟寮忋傜劧鑰岋紝鍋囧鍏╁嬪~瑁滃彇涓妯g殑鍚嶅瓧锛屼絾鏈変笉涓妯g殑濉鍓獢鍚嶏紝濡(mydata.json` and ``mydata.xml鍦ㄥ悓涓鍊嬬洰閷勪笅琚櫦鐝)锛屽~瑁滅殑瀹夎鍕曚綔灏囨渻鍙栨秷锛岀劧寰屽叾浠栫殑宸插畨瑁濇暩鎿氬湪loaddata鍛煎彨鏅傦紝灏囨渻寰炴暩鎿氬韩琚Щ闄ゆ帀銆
The fixtures that are named can include directory components. These directories will be included in the search path. The following, for example:
宸插懡鍚嶇殑濉鍙互鍖呭惈瀛愮洰閷勶紝閫欎簺鐩寗灏囪 鍖呭惈閫叉悳灏嬬殑璺緫瑁忋傚涓嬶紝鑸変緥
django-admin.py loaddata foo/bar/mydata.json
will search <appname>/fixtures/foo/bar/mydata.json for each installed application, <dirname>/foo/bar/mydata.json for each directory in FIXTURE_DIRS , and the literal path foo/bar/mydata.json .
涓婇潰鐨勮鍙ュ皢浼氭煡璇㈡瘡涓涓凡缁忓畨瑁呯殑搴旂敤鐨刞`<appname>/fixtures/foo/bar/mydata.json`` 锛宍`FIXTURE_DIRS`` 涓殑姣忎竴涓猔`<dirname>/foo/bar/mydata.json`` 锛屼互鍙婂師涔夎矾寰刞`foo/bar/mydata.json`` 鐨勫
Note that the order in which fixture files are processed is undefined. However, all fixture data is installed as a single transaction, so data in one fixture can reference data in another fixture. If the database back-end supports row-level constraints, these constraints will be checked at the end of the transaction.
The dumpdata command can be used to generate input for loaddata .
MySQL and Fixtures
Unfortunately, MySQL isnt capable of completely supporting all the features of Django fixtures. If you use MyISAM tables, MySQL doesnt support transactions or constraints, so you wont get a rollback if multiple transaction files are found, or validation of fixture data. If you use InnoDB tables, you wont be able to have any forward references in your data files MySQL doesnt provide a mechanism to defer checking of row constraints until a transaction is committed.
Executes the equivalent of sqlreset for the given app names.
Starts a set of FastCGI processes suitable for use with any Web server that supports the FastCGI protocol. See Chapter 20 for more about deploying under FastCGI.
This command requires the Python FastCGI module from flup (http://www.djangoproject.com/r/flup/).
姝ゅ懡浠ら渶瑕丳ython鐨凢astCGI妯″潡锛屾潵鑷渇lup鈥濓紙`http://www.djangoproject.com/r/flup/`锛夈
Starts a lightweight development Web server on the local machine. By default, the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an IP address and port number explicitly.
鍦ㄦ湰鍦板惎鍔ㄨ交閲忕骇鐨勫紑鍙慦eb鏈嶅姟鍣ㄣ傞粯璁ゆ儏鍐典笅锛岃鏈嶅姟鍣ㄧ洃鍚127.0.0.1鐨8000绔彛锛屽彲浠ヤ紶鍏ュ弬鏁版寚瀹氱洃鍚殑IP鍦板潃涓庣鍙e彿
If you run this script as a user with normal privileges (recommended), you might not have access to start a port on a low port number. Low port numbers are reserved for the superuser (root).
濡傛灉浣犱娇鐢ㄦ櫘閫氱敤鎴锋潈闄愯繍琛岃鍛戒护锛堟帹鑽愭柟寮忥級锛屼綘鍙兘浼氭病鏈夋潈闄愭潵鐩戝惉浣庣鍙c備綆绔彛寰寰鍙湁瓒呯骇鐢ㄦ埛锛坮oot锛夋墠鑳界洃鍚
Warning
娉ㄦ剰锛
Do not use this server in a production setting . It has not gone through security audits or performance tests, and there are no plans to change that fact. Djangos developers are in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.
涓嶈鍦ㄦ渶缁堜骇鍝佷腑浣跨敤璇ユ湇鍔″櫒 . 璇ユ湇鍔″櫒娌℃湁閫氳繃瀹夊叏涓庢ц兘娴嬭瘯锛屽苟涓斾篃涓嶆墦绠楅氳繃銆侱jango寮鍙戣呯殑涓昏浠诲姟鏄埗浣渨eb妗嗘灦锛岃屼笉鏄痺eb鏈嶅姟鍣紝鏀硅繘璇ユ湇鍔″櫒浣夸箣鍙互鍦ㄦ渶缁堜骇鍝佷腑搴旂敤瓒呭嚭浜咲jango鐨勮寖鍥淬
The development server automatically reloads Python code for each request, as needed. You dont need to restart the server for code changes to take effect.
鍦ㄩ渶瑕佺殑鏃跺欙紝璇ュ紑鍙戞湇鍔″櫒浼氫负姣忎釜璇锋眰鑷姩閲嶆柊鍔犺浇Python浠g爜銆傛墍浠ュ綋浣犳敼鍔ㄤ唬鐮佷箣鍚庝笉闇瑕侀噸鏂板惎鍔ㄥ畠灏卞彲浠ョ敓鏁堛
When you start the server, and each time you change Python code while the server is running, the server will validate all of your installed models. (See the upcoming section on the validate command.) If the validator finds errors, it will print them to standard output, but it wont stop the server.
璇ユ湇鍔″櫒鍚姩鍚庯紝鍦ㄦ湇鍔″櫒杩愯鐨勫悓鏃舵洿鏀筆ython浠g爜鏃讹紝璇ユ湇鍔″櫒浼氶獙璇佷綘瀹夎鐨勬墍鏈夋ā鍧楋紙鍙傝冮┈涓婂氨瑕佽鍒扮殑 validate 鍛戒护锛. 濡傛灉鍙戠幇閿欒锛屾湇鍔″櫒浼氭妸瀹冧滑杈撳嚭鍒版爣鍑嗚緭鍑猴紝浣嗘槸鏈嶅姟鍣ㄥ苟涓嶄細鍋滄銆
You can run as many servers as you want, as long as theyre on separate ports. Just execute django-admin.py runserver more than once.
浣犲彲浠ュ悓鏃惰繍琛岃澶氫釜鏈嶅姟鍣ㄥ疄渚嬶紝鍙瀹冧滑鍚勮嚜鐩戝惉涓嶅悓鐨勭鍙c傝杩愯澶氫釜鏈嶅姟鍣ㄥ疄渚嬶紝鍙澶氭鎵ц django-admin.py runserver 灏卞彲浠ヤ簡銆
Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g., 192.168.2.1) or 0.0.0.0.
鍊煎緱涓鎻愮殑鏄粯璁ょ殑IP鍦板潃 127.0.0.1 鏃犳硶浠庣綉缁滀笂鐨勫叾浠栨満鍣ㄨ闂埌锛岃浣挎湇鍔″櫒鍙互琚綉缁滀腑鐨勫叾浠栨湇鍔″櫒璁块棶鍒帮紝浣跨敤鐪熷疄IP鍦板潃锛堜緥濡192.168.2.1锛夋垨鑰0.0.0.0
For example, to run the server on port 7000 on IP address 127.0.0.1, use this:
渚嬪锛岃鍦127.0.0.1鐨7000绔彛杩愯璇ユ湇鍔″櫒锛屼娇鐢ㄥ涓嬫柟娉曪細
django-admin.py runserver 7000
Or to run the server on port 7000 on IP address 1.2.3.4, use this:
鎴栬呭湪IP鍦板潃1.2.3.4鐨7000绔彛杩愯锛屼娇鐢細
django-admin.py runserver 1.2.3.4:7000
By default, the development server doesnt serve any static files for your site (such as CSS files, images, things under MEDIA_ROOT_URL , etc.). If you want to configure Django to serve static media, read about serving static media at http://www.djangoproject.com/documentation/0.96/static_files/.
寮鍙戜腑鐨勬湇鍔″櫒榛樿涓嶅浣犵珯鐐圭殑浠讳綍闈欐佹枃浠舵彁渚涙湇鍔★紙濡侰SS鏂囦欢锛屽浘鐗囷紝鍦ㄢ淢EDIA_ROOT_URL”涓嬬殑鏂囦欢锛岀瓑绛夛級銆傚鏋滆鎸囧畾Django瀵硅繖浜涢潤鎬佹枃浠舵湇鍔★紝璇峰弬闃卄http://www.djangoproject.com/documentation/0.96/static_files/`銆
To disable autoreloading of code while the development server is running, use the --noreload option, like so:
鍦ㄥ紑鍙戞湇鍔″櫒杩愯鎯呭喌涓嬶紝濡傛灉瑕佸叧闂唬鐮佽嚜鍔ㄨ浇鍏ワ紝鐢 --noreload 閫夐」锛 鍍忚繖鏍凤細
django-admin.py runserver --noreload
Starts the Python interactive interpreter.
鍚姩Python浜や簰瑙i噴鍣.
Django will use IPython (http://ipython.scipy.org/) if its installed. If you have IPython installed and want to force use of the plain Python interpreter, use the --plain option, like so:
Django灏嗕娇鐢↖Python锛坔ttp://ipython.scipy.org/锛夛紙濡傛灉宸茬粡瀹夎浜咺Python锛夈傚鏋滃簲缁忓畨瑁呬簡IPython锛屼絾鏄兂浣跨敤骞冲父鐨凱ython瑙i噴鍣紝鍙互浣跨敤’‘—plain’‘閫夐」锛屽氨鍍忚繖鏍凤細
django-admin.py shell --plain
Prints the CREATE TABLE SQL statements for the given app names.
涓烘寚瀹氬簲鐢ㄥ悕绉版墦鍗扳淐REATE TABLE鈥 鐨剆ql澹版槑銆
Prints the CREATE TABLE and initial-data SQL statements for the given app names.
涓烘寚瀹氬簲鐢ㄥ悕绉版墦鍗扳淐REATE TABLE鈥濆拰鍒濆鍖栨暟鎹殑sql澹版槑銆
Refer to the description of sqlcustom for an explanation of how to specify initial data.
鍙傝鈥渟qlcustom鈥濆叧浜庡浣曟寚瀹氬垵濮嬪寲鏁版嵁鐨勮В閲婃弿杩般
Prints the DROP TABLE SQL statements for the given app names.
涓虹粰瀹氱殑搴旂敤鍚嶆墦鍗癭`DROP TABLE``SQL璇彞銆
Prints the custom SQL statements for the given app names.
涓虹粰瀹氬簲鐢ㄥ悕绉版墦鍗拌嚜瀹氫箟鐨剆ql澹版槑銆
For each model in each specified app, this command looks for the file <appname>/sql/<modelname>.sql , where <appname> is the given app name and <modelname> is the models name in lowercase. For example, if you have an app news that includes a Story model, sqlcustom will attempt to read a file news/sql/story.sql and append it to the output of this command.
瀵逛簬姣忎釜鎸囧畾搴旂敤涓殑姣忎釜妯″瀷锛屾鍛戒护鏌ユ壘鏂囦欢鈥<appname>/sql/<modelname>.sql鈥濓紝杩欓噷鈥<appname>鈥濇槸缁欏畾鐨勫簲鐢ㄥ悕绉帮紝鈥<modelname>鈥濇槸灏忓啓鐨勬ā鍨嬪悕绉般傛瘮濡傦紝濡傛灉浣犳湁涓涓簲鐢ㄢ渘ews鈥濓紝鍖呭惈涓涓淪tory鈥濇ā鍨嬶紝閭d箞”sqlcustom”灏嗕細灏濊瘯璇诲彇鏂囦欢鈥渘ews/sql/story.sql鈥濆苟杩藉姞鍏跺埌姝ゅ懡浠ょ殑杈撳嚭銆
Each of the SQL files, if given, is expected to contain valid SQL. The SQL files are piped directly into the database after all of the models table-creation statements have been executed. Use this SQL hook to make any table modifications, or insert any SQL functions into the database.
姣忎釜缁欏嚭鐨剆ql鏂囦欢搴旇鍖呭惈鍚堟硶鐨剆ql銆傚綋鎵鏈夌殑妯″瀷鏁版嵁琛ㄥ垱寤哄0鏄庨兘宸茬粡鎵ц瀹屾垚鏃秙ql鏂囦欢浼氱洿鎺ヤ紶閫佽嚦鏁版嵁搴撱備娇鐢ㄨ繖涓猻ql閽╁瓙浠ヤ慨鏀逛换浣曟暟鎹〃锛屾垨鑰呰緭鍏ヤ换浣晄ql鍑芥暟鍒版暟鎹簱銆
Note that the order in which the SQL files are processed is undefined.
娉ㄦ剰锛歋QL鏂囦欢鐨勫鐞嗛『搴忓苟娌℃湁瀹氫箟銆
Prints the CREATE INDEX SQL statements for the given app names.
涓虹粰瀹氱殑搴旂敤鍚嶆墦鍗 CREATE INDEX SQL 璇彞.
Prints the DROP TABLE SQL, and then the CREATE TABLE SQL, for the given app names.
瀵圭粰瀹氱殑搴旂敤鍚嶏紝鎵撳嵃``DROP TABLE``SQL璇彞锛岀劧鍚庢槸``CREATE TABLE``SQL璇彞銆
Prints the SQL statements for resetting sequences for the given app names.
瀵圭粰瀹氱殑搴旂敤鍚嶏紝鎵撳嵃閲嶇疆搴忓垪鐨凷QL璇彞銆
Youll need this SQL only if youre using PostgreSQL and have inserted data by hand. When you do that, PostgreSQLs primary key sequences can get out of sync from whats in the database, and the SQL emitted by this command will clear it up.
鍙湁褰撲綘浣跨敤PostgreSQL骞朵笖宸茬粡鎵嬪姩杈撳叆鏁版嵁鏃讹紝浣犲皢闇瑕佽繖浜泂ql銆傚綋浣犻偅鏍峰仛鏃讹紝PostgreSQL鐨勪富閿簭鍒椾笉浼氫粠鏁版嵁搴撳悓姝ワ紝骞朵笖姝ゅ懡浠ゆ墍鎻愪氦鐨剆ql灏嗕細娓呴櫎瀹冦
Creates a Django application directory structure for the given app name in the current directory.
鍦ㄥ綋鍓嶇洰褰曚负缁欏畾鐨勫簲鐢ㄥ悕鍒涘缓Django搴旂敤绋嬪簭鐩綍缁撴瀯.
Creates a Django project directory structure for the given project name in the current directory.
鍦ㄥ綋鍓嶇洰褰曚负缁欏畾鐨勯」鐩悕鍒涘缓Django椤圭洰鐩綍缁撴瀯.
Creates the database tables for all applications in INSTALLED_APPS whose tables have not already been created.
鐐烘墍鏈夌殑鎳夌敤鍦╜`INSTALLED_APPS``涓笉瀛樺湪鐨勮〃,鍓靛缓鏁告摎搴〃
Use this command when youve added new applications to your project and want to install them in the database. This includes any applications shipped with Django that might be in INSTALLED_APPS by default. When you start a new project, run this command to install the default applications.
鐢ㄩ欏懡浠ょ暥浣犲鍔犳柊鐨勬噳鐢ㄥ埌浣犵殑闋呯洰,鎯宠瀹夎浠栧戝埌鏁告摎搴,
鐣朵綘闁嬪涓鍊嬫柊鐨勯爡鐩,閬嬭閫欏嬪懡浠ゅ畨瑁濆埌榛樿獚鐨勬噳鐢
If youre installing the django.contrib.auth application, syncdb will give you the option of creating a superuser immediately. syncdb will also search for and install any fixture named initial_data . See the documentation for loaddata for details on the specification of fixture data files.
濡傛灉浣犳鍦ㄥ畨瑁呪渄jango.contrib.auth鈥濆簲鐢紝鈥渟yncdb鈥濆皢浼氱粰鍑虹珛鍗冲垱寤鸿秴绾х敤鎴风殑閫夐」銆傗渟yncdb鈥濅篃灏嗕細鎼滅储骞跺畨瑁呬换浣曞懡鍚嶄负鈥渋nitial_data鈥濈殑澶瑰叿 銆傚叧浜庡す鍏锋暟鎹枃浠剁殑瑙勮寖鍙傝鈥渓oaddata鈥濇枃妗c
娴嬭瘯 銆傘傘傘
Discovers and runs tests for all installed models. Testing was still under development when this book was being written, so to learn more youll need to read the documentation online at http://www.djangoproject.com/documentation/0.96/testing/.
涓烘墍鏈夊凡瀹夎妯″瀷鍙戠幇鍜屾墽琛屾祴璇曘傚綋鍐欎綔鏈功鏃舵祴璇曞伐浣滀粛鐒跺浜庡紑鍙戦樁娈碉紝鍥犳鎯充簡瑙f洿澶氫綘闇瑕侀槄璇诲湪绾挎枃妗o細`http://www.djangoproject.com/documentation/0.96/testing/` 銆
楠岃瘉
Validates all installed models (according to the INSTALLED_APPS setting) and prints validation errors to standard output.
鏍规嵁 INSTALLED_APPS 鐨勮缃, 楠岃瘉鎵鏈夌殑瀹夎妯″潡, 骞跺皢 楠岃瘉閿欒鎵撳嵃鍒版爣鍑嗚緭鍑轰笂.
The sections that follow outline the options that django-admin.py can take.
涓嬮潰鐨勮繖浜涜妭灏嗕細鍒椾妇 django-admin.py 宸ュ叿鍙互甯︾殑鍚勪釜閫夐」.
瑷疆
Example usage:
绀轰緥鐢ㄦ硶:
django-admin.py syncdb --settings=mysite.settings
Explicitly specifies the settings module to use. The settings module should be in Python package syntax (e.g., mysite.settings ). If this isnt provided, django-admin.py will use the DJANGO_SETTINGS_MODULE environment variable.
鏄剧ず鍦版寚瀹氭墍鐢ㄧ殑璁剧疆妯″潡銆傝璁剧疆妯″潡搴旇閬靛惊 Python鍖呰娉曪紙渚嬪锛屸渕ysite.settings鈥濓級銆傚鏋滀笉鎸囧畾锛屸渄jango-admin.py鈥濆皢浼氫娇鐢ㄧ幆澧冨彉閲忊淒JANGO_SETTINGS_MODULE鈥濄
Note that this option is unnecessary in manage.py , because it takes care of setting DJANGO_SETTINGS_MODULE for you.
娉ㄦ剰杩欎釜閫夐」鍦 manage.py 涓笉鏄繀椤荤殑, 鍥犱负瀹冭礋璐d负鎮ㄨ瀹歚`DJANGO_SETTINGS_MODULE`` .
python鐨勭洰褰
Example usage:
绀轰緥鐢ㄦ硶:
django-admin.py syncdb --pythonpath='/home/djangoprojects/myproject'
Adds the given filesystem path to the Python import search path. If this isnt provided, django-admin.py will use the PYTHONPATH environment variable.
娣诲姞缁欏畾璺緞鍒癙ython鐨勫鍏ユ悳绱㈣矾寰. 濡傛灉娌℃湁鎻愪緵, django-admin.py 灏嗕娇鐢 PYTHONPATH 鐜鍙橀噺.
Note that this option is unnecessary in manage.py , because it takes care of setting the Python path for you.
娉ㄦ剰, 杩欎釜閫夐」鍦 manage.py 涓笉鏄繀椤荤殑, 鍥犲畠璐熻矗涓烘偍璁惧畾Python璺緞.
鏍煎紡鍖
Example usage:
绀轰緥鐢ㄦ硶:
django-admin.py dumpdata --format=xml
Specifies the output format that will be used. The name provided must be the name of a registered serializer.
鎸囧畾涓涓浣跨敤鐨勮緭鍑烘牸寮. 鎻愪緵鐨勫悕瀛楀繀椤绘槸涓涓敞鍐岀殑serializer鐨勫悕瀛.
Displays a help message that includes a terse list of all available actions and options.
鏄剧ず涓涓寘鍚墍鏈夊彲鐢ㄥ姛鑳藉拰閫夐」鐨勭畝瑕佸垪琛.
缂╄繘
Example usage:
绀轰緥鐢ㄦ硶:
django-admin.py dumpdata --indent=4
Specifies the number of spaces that will be used for indentation when pretty-printing output. By default, output will not be pretty-printed. Pretty-printing will only be enabled if the indent option is provided.
鎸囧畾鍙嬪ソ杈撳嚭锛堜紭缇庤緭鍑猴紡鏍煎紡鍖栬緭鍑猴級鏃剁敤浜庣缉杩涚殑绌烘牸鏁伴噺銆傞粯璁ょ殑杈撳嚭灏嗕笉浼氭槸鍙嬪ソ杈撳嚭銆傚弸濂借緭鍑哄彧鍦ㄦ彁渚涚缉杩涢夐」鏃跺彲鐢ㄣ
Indicates you will not be prompted for any input. This is useful if the django-admin script will be executed as an unattended, automated script.
琛ㄧず灏嗕笉浼氭彁绀轰綘鍋氫换浣曡緭鍏ャ傝繖涓鐐瑰湪鈥渄jango-admin鈥濊剼鏈綔涓轰竴涓棤浜哄煎畧鐨勮嚜鍔ㄨ剼鏈墽琛屾椂寰堟湁鐢ㄣ
Disables the use of the autoreloader when running the development server.
褰撹繍琛屽紑鍙戞湇鍔″櫒鐨勬椂鍊, 绂佹浣跨敤鑷姩鍔犺浇鍣.
Displays the current Django version.
鏄剧ず褰撳墠鐨凞jango鐗堟湰.
Example output:
绀轰緥杈撳嚭:
0.9.1 0.9.1 (SVN)
Example usage:
绀轰緥鐢ㄦ硶:
django-admin.py syncdb --verbosity=2
Determines the amount of notification and debug information that will be printed to the console. 0 is no output, 1 is normal output, and 2 is verbose output.
纭畾閫氱煡鏁伴噺鍜屾墦鍗板埌鎺у埗鍙扮殑璋冭瘯淇℃伅銆0浠h〃娌℃湁杈撳嚭锛1浠h〃姝e父杈撳嚭锛2浠h〃璇︾粏杈撳嚭銆
Example usage:
绀轰緥鐢ㄦ硶:
django-admin.py --adminmedia=/tmp/new-admin-style/
Tells Django where to find the various CSS and JavaScript files for the admin interface when running the development server. Normally these files are served out of the Django source tree, but because some designers customize these files for their site, this option allows you to test against custom versions.
鐢ㄦ潵鍛婅瘔Django褰撲娇鐢ㄨ嚜甯︾殑寮鍙戞湇鍔″櫒鐨勬椂鍊欙紝濡備綍涓篴dmin鐣岄潰鍘诲鎵句笉鍚岀殑CSS鍜孞avaScript鏂囦欢銆傞氬父杩欎簺鏂囦欢閮芥槸瀛樻斁鍦―jango鐨勬簮浠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.