Django学习笔记--制作可重用的web apps
2015-05-13 15:13
204 查看
制作可重用的web apps
所谓可重用的apps,是指把开发的web apps制作成独立的packege,就像用pip安装的那些app一样。这样的web app package经过简单的配置就能整合进web工程里面直接使用。下面用一个例子说明这个过程,假如这个app名为polls
需要用到两个工具setuptools和pip
1. 为web app新建一个父目录
例子中,给polls新建父目录django-polls2. 把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.9Django 1.8.1
参考资料:
https://docs.djangoproject.com/en/1.8/intro/reusable-apps/相关文章推荐
- Django学习笔记2
- Django学习笔记
- Django 开发学习笔记(8)- 开发表单页面,让用户添加内容
- Django1.8学习笔记总结(基础+settings)
- Django学习笔记(二)创建一个简单页面
- python的web开发框架django学习笔记
- django学习笔记
- Django学习笔记1-创建Django项目
- django基础学习笔记
- Django Web开发学习笔记(4)
- Django学习笔记5--表单
- (JQuery学习笔记)利用jquery制作网页导航定位
- Django学习笔记5 让用户拥有私有数据 访问限制
- OAF学习笔记-20-在系统页面制作Tab页(系统中有Tab页,自己增加Tab)
- Unity学习笔记2 简易2D横版RPG游戏制作(二)
- Django REST Framework 学习笔记
- Django框架学习笔记(29.kindeditor插件使用)
- Golang学习笔记:包制作
- OAF学习笔记-2-下拉框的制作
- The Definitive Guide To Django 2 学习笔记(五) 第四章 模板 (一)基本模板系统