We think its best to get a running start. The details and extent of the Django framework will be fleshed out in the later chapters, but for now, trust us, this chapter will be fun.
鑹ソ鐨勫紑绔儨杩囦竴鍒囥傚悗缁珷鑺傚皢鍏呮枼鐫 Django 妗嗘灦鐨勭粏鑺傚拰鎷撳睍锛屼笉杩囩幇鍦ㄥ憿锛岃鐩镐俊鎴戜滑锛岃繖涓绔犺繕鏄洰鏈夋剰鎬濈殑銆
Installing Django is easy. Because Django runs anywhere Python does, Django can be configured in many ways. We cover the common scenarios for Django installations in this chapter. Chapter 20 covers deploying Django to production.
Django 瀹夎寰堢畝鍗曘傚洜涓烘墍鏈 Python 鍙繍琛岀殑鍦版柟 Django 閮藉彲浠ヨ繍琛岋紝鎵浠ュ彲浠ラ氳繃澶氱鏂瑰紡閰嶇疆 Django 銆傝繖涓绔犱腑锛屾垜浠皢浠嬬粛涓浜涘父瑙佺殑 Django 瀹夎鏂规銆傜20绔犱腑灏嗕粙缁嶅浣曞皢 Django 閮ㄧ讲涓轰骇鍝併
Django is written in 100% pure Python code, so youll need to install Python on your system. Django requires Python 2.3 or higher.
Django 鐢辩櫨鍒嗙櫨鐨勭函 Python 浠g爜缂栧啓鑰屾垚锛屽洜姝ゅ繀椤诲湪绯荤粺涓畨瑁 Python 銆侱jango 闇瑕 2.3 鎴栨洿楂樼増鏈殑 Python銆
If youre on Linux or Mac OS X, you probably already have Python installed. Type python at a command prompt (or in Terminal, in OS X). If you see something like this, then Python is installed:
濡傛灉浣跨敤鐨勬槸 Linux 鎴 Mac OS X 锛岀郴缁熷彲鑳藉凡缁忛瑁呬簡 Python 銆傚湪鍛戒护鎻愮ず绗︿笅 (鎴 OS X 鐨勭粓绔腑) 杈撳叆 python 锛屽鏋滅湅鍒板涓嬩俊鎭紝璇存槑 Python 宸茬粡瑁呭ソ浜嗭細
Python 2.4.1 (#2, Mar 31 2005, 00:05:10) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Otherwise, if you see an error such as "command not found" , youll have to download and install Python. See http://www.python.org/download/ to get started. The installation is fast and easy.
涓嶇劧锛屽鏋滅湅瑙佺被浼 "command not found" 鐨勯敊璇紝浣犲氨涓嶅緱涓嶄笅杞藉畨瑁 Python 浜嗐傚弬闃 http://www.python.org/download/ 鑾峰彇鐩稿叧鍏ラ棬鐭ヨ瘑銆 Python 鐨勫畨瑁 绠鍗曡屽揩鎹枫
In this section, we cover two installation options: installing an official release and installing from Subversion.
鏈妭涓紝鎴戜滑浠嬬粛涓ょ瀹夎鏂瑰紡锛氬畼鏂瑰彂甯冪増瀹夎鍜岄氳繃 Subversion 瀹夎銆
Most people will want to install the latest official release from http://www.djangoproject.com/download/. Django uses the standard Python distutils installation method, which in Linux land looks like this:
澶у鏁颁汉浼氳冭檻浠 http://www.djangoproject.com/download/ 涓嬭浇瀹夎鏈鏂扮殑瀹樻柟鍙戝竷鐗堛侱jango 浣跨敤浜 Python 鏍囧噯鐨 distutils 瀹夎娉曪紝鍦 Linux 骞冲彴鍙兘鍖呮嫭濡備笅姝ラ锛
Download the tarball, which will be named something like Django-0.96.tar.gz .
涓嬭浇 tar 瀹夎鍖咃紝鍏舵枃浠跺悕鍙兘浼氭槸 Django-0.96.tar.gz 銆
tar xzvf Django-*.tar.gz .
tar xzvf Django-*.tar.gz 銆
cd Django-* .
cd Django-* 銆
sudo python setup.py install .
sudo python setup.py install 銆
On Windows, we recommend using 7-Zip to handle all manner of compressed files, including .tar.gz . You can download 7-Zip from http://www.djangoproject.com/r/7zip/.
鍦╳indows骞冲彴涓嬶紝鎴戜滑寤鸿浣跨敤 7-Zip 鏉ュ鐞嗗悇绉嶆牸寮忕殑鍘嬬缉鏂囨。锛屽寘鎷 “.tar.gz” 鏍煎紡銆7-Zip 鍙互浠 http://www.djangoproject.com/r/7zip/ 涓嬭浇寰楀埌 銆
Change into some other directory and start python . If everything worked, you should be able to import the module django :
鍒囨崲鍒板彟涓涓洰褰曞苟鍚姩 python 銆傚鏋滀竴鍒囧氨缁紝浣犲氨鍙互瀵煎叆 django 妯″潡浜嗭細
>>> import django >>> django.VERSION (0, 96, None)
Note
娉ㄦ剰
The Python interactive interpreter is a command-line program that lets you write a Python program interactively. To start it, just run the command python at the command line. Throughout this book, we feature example Python code thats printed as if its being entered in the interactive interpreter. The triple greater-than signs (>>> ) signify a Python prompt.
Python 浜や簰瑙i噴鍣ㄦ槸鍛戒护琛岀獥鍙g殑绋嬪簭锛岄氳繃瀹冨彲浠ヤ氦浜掑紡缂栧啓 Python 绋嬪簭銆 瑕佸惎鍔ㄥ畠鍙渶杩愯 python 鍛戒护銆傛湰涔︽墍鏈夌殑 Python 绀轰緥浠g爜鍧囦互鍦ㄤ氦浜掕В閲婂櫒涓緭鍏ョ殑鏂瑰紡灞曠ず銆傞偅涓変釜澶т簬鍙凤紙 >>> 锛夋槸 Python 鐨勬彁绀虹銆
If you want to work on the bleeding edge, or if you want to contribute code to Django itself, you should install Django from its Subversion repository.
濡傛灉鎯充綋楠屾湁涓瀹氶闄╃殑鏂扮壒鎬э紝鎴栬呮兂涓 Django 璐$尞浠g爜鐨勮瘽锛屽簲璇ヤ粠Subverion 浠撳簱涓嬭浇瀹夎 Django銆
Subversion is a free, open source revision-control system similar to CVS, and the Django team uses it to manage changes to the Django codebase. You can use a Subversion client to grab the very latest Django source code and, at any given time, you can update your local version of the Django code, known as your local checkout , to get the latest changes and improvements made by Django developers.
Subversion 鏄竴绉嶄笌 CVS 绫讳技鐨勫厤璐瑰紑婧愮増鏈帶鍒剁郴缁燂紝Django 寮鍙戝洟闃熶娇鐢ㄥ畠绠$悊 Django 浠g爜搴撶殑鏇存柊銆備綘鍙互浣跨敤 Subversion 瀹㈡埛绔幏鍙栨渶鏂扮殑 Django 婧愪唬鐮侊紝骞跺彲浠讳綍鏃跺欎娇鐢 local checkout 鏇存柊鏈湴 Django 浠g爜鐨勭増鏈紝浠ヨ幏鍙 Django 寮鍙戣呮墍鍋氱殑鏈杩戞洿鏂板拰鏀硅繘銆
The latest and greatest Django development code is referred to as the trunk . The Django team runs production sites on trunk and strives to keep it stable.
鏈鏂板拰鏈濂界殑Django浠g爜閫氬父鍙仛*trunk锛堜富骞诧級*銆侱jango寮鍙戝洟闃熷氨鏄娇鐢╰runk浠g爜鏉ヨ繍琛屼骇鍝佺骇绔欑偣锛屽苟鍏ㄥ姏纭繚鍏剁ǔ瀹氭с
To grab the latest Django trunk, follow these steps:
閬靛惊浠ヤ笅姝ラ浠ヨ幏鍙栨渶鏂扮殑 Django 涓绘祦浠g爜锛
Make sure you have a Subversion client installed. You can get the software free from http://subversion.tigris.org/, and you can find excellent documentation at http://svnbook.red-bean.com/.
纭繚瀹夎浜 Subversion 瀹㈡埛绔傚彲浠ヤ粠 http://subversion.tigris.org/ 鍏嶈垂涓嬭浇璇ヨ蒋浠讹紝骞朵粠 http://svnbook.red-bean.com/ 鑾峰彇鍑鸿壊鐨勬枃妗c
Check out the trunk using the command svn co http://code.djangoproject.com/svn/django/trunk djtrunk .
浣跨敤 svn co http://code.djangoproject.com/svn/django/trunk djtrunk 鍛戒护妫鍑轰富娴佷唬鐮併
Create site-packages/django.pth and add the djtrunk directory to it, or update your PYTHONPATH to point to djtrunk .
鍒涘缓 site-packages/django.pth 骞跺皢 djtrunk 鐩綍娣诲姞杩涘幓锛屾垨鑰呮洿鏂 PYTHONPATH 璁剧疆锛屽皢鍏舵寚鍚 djtrunk 銆
Place djtrunk/django/bin on your system PATH. This directory includes management utilities such as django-admin.py .
灏 djtrunk/django/bin 鍔犲叆绯荤粺鍙橀噺 PATH 涓傝鐩綍涓寘鍚竴浜涘儚 django-admin.py 涔嬬被鐨勭鐞嗗伐鍏枫
Tip:
鎻愮ず锛
If .pth files are new to you, you can learn more about them at http://www.djangoproject.com/r/python/site-module/.
濡傛灉涔嬪墠娌℃湁鎺ヨЕ杩 .pth 鏂囦欢锛屼綘鍙互浠 http://www.djangoproject.com/r/python/site-module/ 涓幏鍙栨洿澶氱浉鍏崇煡璇嗐
After downloading from Subversion and following the preceding steps, theres no need to python setup.py install youve just done the work by hand!
浠 Subversion 瀹屾垚涓嬭浇骞舵墽琛屼簡鍓嶈堪姝ラ鍚庯紝灏辨病鏈夊繀瑕佸啀鎵ц python setup.py install 浜嗭紝浣犲垰鎵嶅凡缁忔墜鍔ㄥ畬鎴愪簡瀹夎锛
Because the Django trunk changes often with bug fixes and feature additions, youll probably want to update it every once in a while or hourly, if youre really obsessed. To update the code, just run the command svn update from within the djtrunk directory. When you run that command, Subversion will contact `http://code.djangoproject.com`_, determine if any code has changed, and update your local version of the code with any changes that have been made since you last updated. Its quite slick.
鐢变簬 Django 涓诲共浠g爜鐨勬洿鏂扮粡甯稿寘鎷 bug 淇鍜岀壒鎬ф坊鍔狅紝濡傛灉鐪熺殑鐫杩风殑璇濓紝浣犲彲鑳芥瘡闅斾竴灏忔鏃堕棿灏辨兂鏇存柊涓娆°傚湪 djtrunk 鐩綍涓嬭繍琛 svn update 鍛戒护鍗冲彲杩涜鏇存柊銆傚綋浣犱娇鐢ㄨ繖涓懡浠ゆ椂锛孲ubversion 浼氳仈缁 http://code.djangoproject.com 锛屽垽鏂唬鐮佹槸鍚︽湁鏇存柊锛岀劧鍚庢妸涓婃鏇存柊浠ユ潵鐨勬墍鏈夊彉鍔ㄥ簲鐢ㄥ埌鏈湴浠g爜銆傚氨杩欎箞绠鍗曘
Djangos only prerequisite is a working installation of Python. However, this book focuses on one of Djangos sweet spots, which is developing database-backed Web sites, so youll need to install a database server of some sort, for storing your data.
浣跨敤 Django 鐨勫敮涓鍏堝喅鏉′欢鏄畨瑁 Python銆傜劧鑰岋紝鏈功鎵鍏虫敞鐨勬槸 Django 鐨勪寒鐐逛箣涓鈥斺斿熀浜庢暟鎹簱鐨勭綉绔欏紑鍙戯紝鍥犳浣犲繀椤诲畨瑁呮煇绉嶇被鍨嬬殑鏁版嵁搴撴潵瀛樺偍鏁版嵁銆
If you just want to get started playing with Django, skip ahead to the Starting a Project sectionbut trust us, youll want to install a database eventually. All of the examples in the book assume you have a database set up.
濡傛灉鍙槸鎯冲紑濮嬫憜寮 Django 锛屽彲浠ヨ烦鍒般婂紑濮嬩竴涓」鐩嬪皬鑺傘備絾璇风浉淇℃垜浠紝鍒版渶鍚庝綘杩樻槸浼氭兂瑕佸畨瑁呬竴涓暟鎹簱銆傛湰涔︿腑鎵鏈変緥瀛愰兘鍋囧畾浣犲畨瑁呬簡涓涓暟鎹簱绯荤粺銆
As of the time of this writing, Django supports three database engines:
鏈功鍐欎綔鏃讹紝Django 鏀寔 3 绉嶆暟鎹簱寮曟搸锛
PostgreSQL (http://www.postgresql.org/)
PostgreSQL (http://www.postgresql.org/)
SQLite 3 (http://www.sqlite.org/)
SQLite 3 (http://www.sqlite.org/)
MySQL (http://www.mysql.com/)
MySQL (http://www.mysql.com/)
Work is in progress to support Microsoft SQL Server and Oracle. The Django Web site will always have the latest information about supported databases.
鏈変汉姝e湪鍔姏璁╁畠鑳藉鏀寔 SQL Server 鍜 Oracle 銆侱jango 瀹樻柟缃戠珯浼氱粡甯稿彂甯冩墍鏀寔鏁版嵁搴撶殑鏈鏂版秷鎭
Were quite fond of PostgreSQL ourselves, for reasons outside the scope of this book, so we mention it first. However, all the engines listed here will work equally well with Django.
鍑轰簬鏈功鎵璁ㄨ鑼冨洿涔嬪鐨勫師鍥狅紝鎴戜滑闈炲父鍋忕埍 PostgreSQL锛屽洜姝ゆ垜浠皢绗竴涓彁鍒板畠銆傚綋鐒讹紝杩欓噷鎵鍒楀嚭鐨勫紩鎿庡拰 Django 閮介厤鍚堝緱涓鏍峰ソ銆
SQLite deserves special notice as a development tool. Its an extremely simple in-process database engine that doesnt require any sort of server setup or configuration. Its by far the easiest to set up if you just want to play around with Django, and its even included in the standard library of Python 2.5.
浣滀负寮鍙戝伐鍏凤紝SQLite 鐗瑰埆鍊煎緱娉ㄦ剰銆傚畠鏄竴绉嶉潪甯哥畝鍗曠殑杩涚▼绾ф暟鎹簱寮曟搸锛屾棤闇浠讳綍鏈嶅姟鍣ㄥ畨瑁呮垨閰嶇疆宸ヤ綔銆傚鏋滀綘鍙槸鎯虫憜寮勪竴涓 Django 锛屽畠鏄埌鐩墠涓烘鏈瀹规槗瀹夎鐨勶紝鐢氳嚦宸茬粡琚寘鎷湪 Python 2.5 鐨勬爣鍑嗙被搴撲箣涓
On Windows, obtaining database driver binaries is sometimes an involved process. Since youre just getting started with Django, we recommend using Python 2.5 and its built-in support for SQLite. Compiling driver binaries is a downer.
鍦 Windows 骞冲彴涓婏紝鑾峰彇鏁版嵁搴撲簩杩涘埗椹卞姩鏄欢寰堟鎵嬬殑宸ヤ綔銆傚洜涓轰綘鍒氬紑濮嬫帴瑙 Django锛屾垜浠帹鑽愪娇鐢 Python 2.5 鍙婂叾瀵 SQLite 鐨勫唴寤烘敮鎸併傜紪璇戜簩杩涘埗椹卞姩鏄欢浠や汉娌抚鐨勪簨銆
If youre using PostgreSQL, youll need the psycopg package available from http://www.djangoproject.com/r/python-pgsql/. Take note of whether youre using version 1 or 2; youll need this information later.
浣跨敤 PostgreSQL 鐨勮瘽锛屼綘闇瑕佷粠 http://www.djangoproject.com/r/python-pgsql/ 涓嬭浇 psycopg 杩欎釜寮鍙戝寘銆傜暀鎰忎綘鎵鐢ㄧ殑鏄 鐗堟湰 1 杩樻槸 2锛岀◢鍚庝綘浼氶渶瑕佽繖椤逛俊鎭
If youre using PostgreSQL on Windows, you can find precompiled binaries of psycopg at http://www.djangoproject.com/r/python-pgsql/windows/.
濡傛灉鍦 Windows 骞冲彴涓婁娇鐢 PostgreSQL锛屽彲浠ヤ粠 http://www.djangoproject.com/r/python-pgsql/windows/ 鑾峰彇棰勭紪璇戠殑 psycopg 寮鍙戝寘鐨勪簩杩涘埗鏂囦欢銆
If youre using a Python version over 2.5, you already have SQLite. If youre working with Python 2.4 or older, youll need SQLite 3 not version 2from http://www.djangoproject.com/r/sqlite/ and the pysqlite package from http://www.djangoproject.com/r/python-sqlite/. Make sure you have pysqlite version 2.0.3 or higher.
濡傛灉浣跨敤 2.5 鍙婃洿楂樼増鏈殑 Python 锛屼綘鏃犻渶鍐嶅畨瑁 SQLite 銆備絾濡傛灉浣跨敤鐨勬槸 2.4 鎴栬呮洿浣庣増鏈殑 Python锛屼綘鎵闇瑕佺殑 SQLite 3 涓嶆槸浠 http://www.djangoproject.com/r/sqlite/ 涓嬭浇鍒扮殑鐗堟湰 2 浠ュ強浠 http://www.djangoproject.com/r/python-sqlite/ 涓嬭浇 pysqlite 銆傚繀椤荤‘淇濅娇鐢ㄧ殑鏄 2.0.3 鎴栬呮洿楂樼増鏈殑 pysqlite 銆
On Windows, you can skip installing the separate SQLite binaries, since theyre statically linked into the pysqlite binaries.
鍦 Windows 骞冲彴涓婏紝鍙互璺宠繃鍗曠嫭鐨 SQLite 浜岃繘鍒跺寘瀹夎宸ヤ綔锛屽洜涓哄畠浠凡琚潤鎬侀摼鎺ュ埌 pysqlite 浜岃繘鍒跺紑鍙戝寘涓
Django requires MySQL 4.0 or above; the 3.x versions dont support nested subqueries and some other fairly standard SQL statements. Youll also need the MySQLdb package from http://www.djangoproject.com/r/python-mysql/.
Django 闇瑕 4.0 鎴栬呮洿楂樼増鏈殑 MySQL锛3.x 鐗堜笉鏀寔宓屽瀛愭煡璇互鍙婂叾瀹冧竴浜涚浉褰撴爣鍑嗙殑 SQL 璇彞銆備綘杩橀渶瑕佷粠 http://www.djangoproject.com/r/python-mysql/ 涓嬭浇瀹夎 MySQLdb 銆
As mentioned earlier, Django doesnt actually require a database. If you just want to use it to serve dynamic pages that dont hit a database, thats perfectly fine.
姝e涔嬪墠鎻愬強杩囩殑锛孌jango 骞朵笉鏄潪寰楄鏁版嵁搴撴墠鍙互杩愯銆傚鏋滃彧鐢ㄥ畠鎻愪緵涓浜涗笉娑夊強鏁版嵁搴撶殑鍔ㄦ侀〉闈㈡湇鍔★紝涔熷悓鏍峰彲浠ュ畬缇庤繍琛屻
With that said, bear in mind that some of the extra tools bundled with Django do require a database, so if you choose not to use a database, youll miss out on those features. (We highlight these features throughout this book.)
灏界濡傛锛岃繕鏄璁颁綇锛欴jango 鎵鎹嗙粦鐨勪竴浜涢檮鍔犲伐鍏 涓瀹 闇瑕佹暟鎹簱锛屽洜姝ゅ鏋滈夋嫨涓嶄娇鐢ㄦ暟鎹簱锛屼綘灏嗕笉鑳戒娇鐢ㄨ繖浜涘姛鑳姐 (鎴戜滑浼氬湪鍏ㄤ功涓爣鍑鸿繖浜涘姛鑳姐)
A project is a collection of settings for an instance of Django, including database configuration, Django-specific options, and application-specific settings.
椤圭洰 鏄 Django 瀹炰緥涓绯诲垪璁剧疆鐨勯泦鍚堬紝瀹冨寘鎷暟鎹簱閰嶇疆銆丏jango 鐗瑰畾閫夐」浠ュ強搴旂敤绋嬪簭鐨勭壒瀹氳缃
If this is your first time using Django, youll have to take care of some initial setup. Create a new directory to start working in, perhaps something like /home/username/djcode/ , and change into that directory.
濡傛灉绗竴娆′娇鐢 Django锛屽繀椤昏繘琛屼竴浜涘垵濮嬪寲璁剧疆宸ヤ綔銆傛柊寤轰竴涓伐浣滅洰褰曪紝渚嬪 /home/username/djcode/ 锛岀劧鍚庤繘鍏ヨ鐩綍銆
Note
澶囧繕
django-admin.py should be on your system path if you installed Django via its setup.py utility. If you checked out from Subversion, you can find it in djtrunk/django/bin . Since youll be using django-admin.py often, consider adding it to your path. On Unix, you can do so by symlinking from /usr/local/bin , using a command such as sudo ln -s /path/to/django/bin/django-admin.py /usr/local/bin/django-admin.py . On Windows, youll need to update your PATH environment variable.
濡傛灉鐢ㄧ殑鏄 setup.py 宸ュ叿杩涜鐨 Django 瀹夎锛 django-admin.py 搴旇宸茶鍔犲叆浜嗙郴缁熻矾寰勪腑銆傚鏋滄槸浠 Subversion 妫鍑虹殑浠g爜锛屽垯鍙互鍦 djtrunk/django/bin 涓壘鍒板畠銆傚洜涓轰細缁忓父鐢ㄥ埌 django-admin.py 锛屽彲浠ヨ冭檻鎶婂畠鍔犲叆绯荤粺鎼滅储璺緞銆傚湪 Unix 涓婏紝浣犲彲浠ョ敤 sudo ln -s /path/to/django/bin/django-admin.py /usr/local/bin/django-admin.py 杩欐牱鐨勫懡浠や粠 /usr/local/bin 涓缓绔嬬鍙疯繛鎺ャ傚湪 Windows 骞冲彴涓婂垯闇瑕佹洿鏂 PATH 鐜鍙橀噺銆
Run the command django-admin.py startproject mysite to create a mysite directory in your current directory.
杩愯 django-admin.py startproject mysite 鍛戒护鍦ㄥ綋鍓嶇洰褰曞垱寤轰竴涓 mysite 鐩綍銆
Lets look at what startproject created:
璁╂垜浠湅鐪 startproject 閮藉垱寤轰簡鍝簺鍐呭锛
mysite/ __init__.py manage.py settings.py urls.py
These files are as follows:
鍖呮嫭涓嬪垪杩欎簺鏂囦欢锛
__init__.py : A file required for Python treat the directory as a package (i.e., a group of modules)
__init__.py 锛氳 Python 鎶婅鐩綍褰撴垚涓涓紑鍙戝寘 (鍗充竴缁勬ā鍧)鎵闇鐨勬枃浠躲
manage.py : A command-line utility that lets you interact with this Django project in various ways
manage.py 锛氫竴绉嶅懡浠よ宸ュ叿锛屽彲璁╀綘浠ュ绉嶆柟寮忎笌璇 Django 椤圭洰杩涜浜や簰銆
settings.py : Settings/configuration for this Django project
settings.py 锛氳 Django 椤圭洰鐨勮缃垨閰嶇疆銆
urls.py : The URL declarations for this Django project; a table of contents of your Django-powered site
urls.py 锛氳 Django 椤圭洰鐨 URL 澹版槑锛屽嵆 Django 鎵鏀拺绔欑偣鐨勫唴瀹瑰垪琛
Where Should This Directory Live?
杩欎釜鐩綍搴旇鏀惧摢鍎匡紵
If your background is in PHP, youre probably used to putting code under the Web servers document root (in a place such as /var/www ). With Django, you dont do that. Its not a good idea to put any of this Python code within your Web servers document root, because in doing so you risk the possibility that people will be able to view your code over the Web. Thats not good for security.
鏈夎繃 PHP 缂栫▼鑳屾櫙鐨勮瘽锛屼綘鍙兘涔犳儻浜庡皢浠g爜閮芥斁鍦 Web 鏈嶅姟鍣ㄧ殑鏂囨。鏍圭洰褰 (渚嬪 /var/www 杩欐牱鐨勫湴鏂)銆傝屽湪 Django 涓紝浣犱笉鑳借繖鏍峰仛銆傛妸浠讳綍 Python 浠g爜鏀惧埌 Web 鏈嶅姟鍣ㄧ殑鏂囨。鏍圭洰褰曚腑閮戒笉鏄釜濂戒富鎰忥紝鍥犱负杩欐牱涓鏉ワ紝浣犲氨瑕佸啋鐫鍒汉閫忚繃椤甸潰鐩存帴鐪嬪埌浠g爜鐨勯闄┿傝繖瀵逛簬瀹夊叏鍙笉鏄欢濂戒簨銆
Put your code in some directory outside of the document root.
鎶婁唬鐮佹斁缃湪鏂囨。鏍圭洰褰 涔嬪 鐨勬煇浜涚洰褰曚腑銆
Django includes a built-in, lightweight Web server you can use while developing your site. Weve included this server so you can develop your site rapidly, without having to deal with configuring your production Web server (e.g., Apache) until youre ready for production. This development server watches your code for changes and automatically reloads, helping you make many rapid changes to your project without needing to restart anything.
Django 甯︽湁涓涓唴寤虹殑杞婚噺绾 Web 鏈嶅姟鍣紝鍙緵绔欑偣寮鍙戣繃绋嬩腑浣跨敤銆傛垜浠彁渚涜繖涓湇鍔″櫒鏄负浜嗚浣犲揩閫熷紑鍙戠珯鐐癸紝涔熷氨鏄鍦ㄥ噯澶囧彂甯冧骇鍝佷箣鍓嶏紝鏃犻渶杩涜浜у搧绾 Web 鏈嶅姟鍣紙姣斿 Apache锛夌殑閰嶇疆宸ヤ綔銆傝寮鍙戞湇鍔″櫒浼氱洃娴嬩唬鐮佸彉鍔ㄥ苟灏嗗叾鑷姩閲嶈浇锛岃繖鏍蜂竴鏉ワ紝浣犲彲蹇熻繘琛岄」鐩慨鏀硅屾棤闇浣滀换浣曢噸鍚
Change into the mysite directory, if you havent already, and run the command python manage.py runserver . Youll see something like this:
濡傛灉杩樻病鏈夎繘鍏 mysite 鐩綍鐨勮瘽锛岀幇鍦ㄨ繘鍏ュ叾涓紝骞惰繍琛 python manage.py runserver 鍛戒护銆備綘灏嗙湅鍒板涓嬭緭鍑猴細
Validating models... 0 errors found. Django version 1.0, using settings 'mysite.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Although the development server is extremely nice for, well, development, resist the temptation to use this server in anything resembling a production environment. The development server can handle only a single request at a time reliably, and it has not gone through a security audit of any sort. When the time comes to launch your site, see Chapter 20 for information on how to deploy Django.
灏界瀵逛簬寮鍙戞潵璇达紝杩欎釜寮鍙戞湇鍔″櫒闈炲父寰楁锛屼絾涓瀹氳鎵撴秷鍦ㄤ骇鍝佺骇鐜涓娇鐢ㄨ鏈嶅姟鍣ㄧ殑蹇靛ご銆傚湪鍚屼竴鏃堕棿锛岃鏈嶅姟鍣ㄥ彧鑳藉彲闈犲湴澶勭悊涓娆″崟涓姹傦紝骞朵笖娌℃湁杩涜浠讳綍绫诲瀷鐨勫畨鍏ㄥ璁°傚彂甯冪珯鐐瑰墠锛岃鍙傞槄绗 20 绔犱簡瑙e浣曢儴缃 Django 銆
Changing the Host or the Port
鏇存敼涓绘満鎴栫鍙
By default, the runserver command starts the development server on port 8000, listening only for local connections. If you want to change the servers port, pass it as a command-line argument:
榛樿鎯呭喌涓嬶紝 runserver 鍛戒护鍦 8000 绔彛鍚姩寮鍙戞湇鍔″櫒锛屼笖鍙洃鍚湰鏈鸿繛鎺ャ傝鎯宠鏇存敼鏈嶅姟鍣ㄧ鍙g殑璇濓紝鍙皢绔彛浣滀负鍛戒护琛屽弬鏁颁紶鍏ワ細
python manage.py runserver 8080
You can also change the IP address that the server listens on. This is especially helpful if youd like to share a development site with other developers. The following:
杩樺彲浠ユ敼鍙樻湇鍔″櫒鐩戝惉鐨 IP 鍦板潃銆傝鍜屽叾浠栧紑鍙戜汉鍛樺叡浜悓涓寮鍙戠珯鐐圭殑璇濓紝璇ュ姛鑳界壒鍒湁鐢ㄣ備笅闈㈢殑鍛戒护锛
python manage.py runserver 0.0.0.0:8080
will make Django listen on any network interface, thus allowing other computers to connect to the development server.
浼氳 Django 鐩戝惉鎵鏈夌綉缁滄帴鍙o紝鍥犳涔熷氨璁╁叾瀹冪數鑴戝彲杩炴帴鍒板紑鍙戞湇鍔″櫒浜嗐
Now that the servers running, visit http://127.0.0.1:8000/ with your Web browser. Youll see a Welcome to Django page shaded a pleasant pastel blue. It worked!
鏃㈢劧鏈嶅姟鍣ㄥ凡缁忚繍琛岃捣鏉ヤ簡锛岀幇鍦ㄧ敤缃戦〉娴忚鍣ㄨ闂 http://127.0.0.1:8000/ 銆 浣犲簲璇ュ彲浠ョ湅鍒颁竴涓蹇殑娣¤摑鑹叉墍绗肩僵鐨 Django 娆㈣繋椤甸潰銆備竴鍒囨甯革紒
涓嬩竴绔犺``````````````
Now that you have everything installed and the development server running, in the next chapter youll write some basic code that demonstrates how to serve Web pages using Django.
鏃㈢劧涓鍒囬兘宸茬粡瀹夎瀹屾瘯锛屽紑鍙戞湇鍔″櫒涔熷凡缁忚繍琛岃捣鏉ヤ簡锛屼笅涓绔犱腑锛岃鎴戜滑缂栧啓涓浜涘熀纭浠g爜锛屾紨绀哄浣曚娇鐢 Django 鎻愪緵缃戦〉鏈嶅姟銆
鍏充簬鏈瘎娉ㄧ郴缁
鏈珯浣跨敤涓婁笅鏂囧叧鑱旂殑璇勬敞绯荤粺鏉ユ敹闆嗗弽棣堜俊鎭備笉鍚屼簬涓鑸鏁寸珷鍋氳瘎娉ㄧ殑鍋氭硶锛 鎴戜滑鍏佽浣犲姣忎竴涓嫭绔嬬殑鈥滄枃鏈潡鈥濆仛璇勬敞銆備竴涓滄枃鏈潡鈥濈湅璧锋潵鏄繖鏍风殑锛
涓涓滄枃鏈潡鈥濇槸涓涓钀斤紝涓涓垪琛ㄩ」锛屼竴娈典唬鐮侊紝鎴栬呭叾浠栦竴灏忔鍐呭銆 浣犻変腑瀹冧細楂樹寒搴︽樉绀:
瑕佸鏂囨湰鍧楀仛璇勬敞锛屼綘鍙渶瑕佺偣鍑诲畠鏃佽竟鐨勬爣璇嗗潡:
鎴戜滑浼氫粩缁嗛槄璇绘瘡涓瘎璁猴紝濡傛灉鍙兘鐨勮瘽鎴戜滑涔熶細鎶婅瘎娉ㄨ冭檻鍒版湭鏉ョ殑鐗堟湰涓幓:
濡傛灉浣犳効鎰忎綘鐨勮瘎娉ㄨ閲囩敤锛岃纭繚鐣欎笅浣犵殑鍏ㄥ悕 (娉ㄦ剰涓嶆槸鏄电О鎴栫畝绉帮級
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.