第一部分传送门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 不一样版本之间的区别
**待翻译!**