Django中的一个app应用,实际上就是相当于一个包
最好将一个应用的相关文件(包括模板、静态文件)放在一起,以便用于其它的项目
Django中将app以包(package)的形式重复利用
-
打包app
- 创建新的目录(如django-polls)以存放app(注意该目录名不应该同其它package名相同)
- 将polls文件拷入django-polls文件中
- 在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 ----------- 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.
- 创建LICENSE文件(许可文件)
- 创建安装文件setup.py(setuptools文档)
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', ], )
- 创建MANIFEST.in文件
默认情况下,将创建的包中只会包含与python相关的模块和包,为了将一些其它文件(如templates/、README.rst、LICENSE)也包含进新创建的包中,需要在MANIFEST.in文件中进行指定
include LICENSE include README.rst recursive-include polls/static * recursive-include polls/templates *
- (可选)在新创建的app中包含细节描述文件
创建docs文件夹,并在MANIFEST.in文件中添加一句
recursive-inclued docs *
- 创建包
在命令提示符中输入
> python setup.py sdist
创建的包位于新建的dist文件夹中,名称为django-polls-0.1.tar.gz
-
使用应用包
- 安装该app包(最好在虚拟环境下)
pip install --user django-polls/dist/django-polls-0.1.tar.gz # 注意是自己的相对路径
- 现在就可以使用该app了(可以删去原项目中的polls文件了)
- 卸载包
pip uninstall django-polls
网友评论