制作可重用的web apps
所谓可重用的apps,是指把开发的web apps制作成独立的packege,就像用pip安装的那些app一样。这样的web app package经过简单的配置就能整合进web工程里面直接使用。
下面用一个例子说明这个过程,假如这个app名为polls
需要用到两个工具setuptools和pip
1. 为web app新建一个父目录
例子中,给polls新建父目录django-polls
2. 把web app文件夹复制到新建的父目录下
例子中,polls整个文件夹复制到django-polls中
3. 新建README.rst文件
这个文件描述一下app的基本信息,及使用方法。
例子中,在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.
4. 新建LICENSE文件
说明版权及许可证信息
5. 新建setup.py文件
这个是很重要一步,setuptools这个工具使用这个文件打包app成packag。这个文件的详细写法参考setuptools docs
例子中,setup.py如下形式:
import os
from setuptools import 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=['polls'],
include_package_data=True,
license='BSD License', # example license
description='A simple Django app to conduct Web-based polls.',
long_description=README,
url='http://www.example.com/',
author='Your Name',
author_email='yourname@example.com',
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'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.2',
'Programming Language :: Python :: 3.3',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
)
6. 新建MANIFEST.IN文件
需要这个文件把README.rst、LICENSE或者其他文档说明包含金package中
例子中,如下形式
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
7. 命令行运行python setup.py sdist
这条命令创建一个新文件夹dist,里面放着已经打包好的package
版本:
- Python 2.7.9
- Django 1.8.1