第一部分傳送門css
第二部分傳送門html
第三部分傳送門python
3.2 模型和數據庫Models and databasesgit
3.2.2 查詢操做making queriesgithub
The Python Package Index (PyPI)有大量的現成可用的Python庫。https://www.djangopackages.com
做爲Django的app基地也有大量現成可用的apps。django
包?App? 包是python重用代碼的方式,以目錄的形式體現,須要包含__init__.py文件,採用import的方式導入。 app則是Django專用的包,包含一些通用的Django組件,例如models、tests、urls和views等子模塊。
經過前面的教程,你的項目結構以下:session
mysite/ manage.py mysite/ __init__.py settings.py urls.py wsgi.py polls/ __init__.py admin.py migrations/ __init__.py 0001_initial.py models.py static/ polls/ images/ background.gif style.css templates/ polls/ detail.html index.html results.html tests.py urls.py views.py templates/ admin/ base_site.html
它已經具有的project和app分離的條件。可是還須要一個打包的過程。app
使用setuptools和pip來打包咱們的app。請先安裝他們。
https://pypi.python.org/pypi/setuptools
https://pypi.python.org/pypi/pip工具
打包的意思是讓你的app具備一種特殊的格式,使得它更容易被安裝和使用。
首先,在Django項目外面,爲你的polls應用,準備一個父目錄,取名django-polls;
爲你的app選擇一個合適的名字:
在取名前,去PYPI搜索一下是否有重名或衝突的包已經存在。建議給包名加上「django-」的前綴。名字中最後一個圓點的後面部分在INSTALLED_APPS中必定要獨一無二,不能和任何Django的contrib packages中的重名,例如auth、admin、messages等等你。
建立一個文件django-polls/README.rst,寫入下面的內容:
Polls
Polls is a simple Django app to conduct Web-based polls. For each question, visitors can choose between a fixed number of answers. Detailed documentation is in the "docs" directory. Quick start ----------- 64 Chapter 2. Getting started Django Documentation, Release 1.10.2a1 1. Add "polls" to your INSTALLED_APPS setting like this:: INSTALLED_APPS = [ ... 'polls', ] 2. Include the polls URLconf in your project urls.py like this:: url(r'^polls/', include('polls.urls')), 3. Run `python manage.py migrate` to create the polls models. 4. Start the development server and visit http://127.0.0.1:8000/admin/ to create a poll (you'll need the Admin app enabled). 5. Visit http://127.0.0.1:8000/polls/ to participate in the poll.
django-polls/setup.py
import os from setuptools import find_packages, setup with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme: README = readme.read() # allow setup.py to be run from any path os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) setup( name='django-polls', version='0.1', packages=find_packages(), include_package_data=True, license='BSD License', # example license description='A simple Django app to conduct Web-based polls.', long_description=README, url='https://www.example.com/', author='Your Name', author_email='yourname@example.com', classifiers=[ 'Environment :: Web Environment', 'Framework :: Django', 'Framework :: Django :: X.Y', # replace "X.Y" as appropriate 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', # example license 'Operating System :: OS Independent', 'Programming Language :: Python', # Replace these appropriately if you are stuck on Python 2. 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Topic :: Internet :: WWW/HTTP', 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', ], )
include LICENSE include README.rst recursive-include polls/static * recursive-include polls/templates *
recursive-include docs *
。須要注意的是,若是docs目錄是空的,那麼它不會被打包進去。固然,許多apps經過在線的網站提供文檔閱讀。在安裝包的時候,最好是以我的身份安裝,而不是全系統範圍的身份。這樣能夠有效減小給別的用戶帶去的影響或被別的用戶影響。固然,最好的方式是在virtualenv環境下,相似隔離的沙盒環境。
pip install --user django-polls/dist/django-polls-0.1.tar.gz
pip uninstall django-polls
你能夠:
https://packaging.python.org/distributing/#uploading-your-project-to-pypi是如何上傳到PyPI的教程。
前面,咱們安裝polls應用做爲一個用戶庫,它有一些缺點:
解決這個問題最好的辦法就是使用virtualenv。詳見https://virtualenv.pypa.io/en/stable/
本節主要介紹Django文檔的劃分,各部分的側重點,如何找到本身感興趣的內容。
因爲此部分和文檔最前面的目錄導航重複較多,而且比較簡單,就不翻譯了。
2.11.1 在文檔中查找
2.11.2 文檔是如何組織的
2.11.3 文檔是如何更新的
2.11.4 從哪裏獲取文檔
2.11.5 不一樣版本之間的區別
**待翻譯!**