Django H1 高级教程:如何编写可重用的应用

http://python.usyiyi.cn/translate/django_182/intro/reusable-apps.html

可重用很重要

  你在教程 2中创建了mysite/templates ,在教程 3中创建了polls/templates。 现在你可能更加清晰为什么我们为项目和应用选择单独的模板目录:属于投票应用的部分全部在polls中。它使得该应用“自包含(self-contained)”且更加容易丢到一个新的项目中。

  Python 打包的目前状态因为有多种工具而混乱不堪。对于本教程,我们打算使用setuptools来构建我们的包。它是推荐的打包工具(已经与distribute 分支合并)  

打包你的应用

step 1 

  mkdir django-tasks

  mv tasks django-tasks

$ cat django-tasks/MANIFEST.in 
include LICENSE
include README.rst
recursive-include tasks/static *
recursive-include tasks/templates *
recursive-include docs *

$mkdir django-tasks/doc

$touch django-tasks/README.rst

$$ cat django-tasks/setup.py 
#!/usr/bin/env python
#-*- coding: utf-8 -*-

__author__ = 'Ye Lin'
__date__ = '17-2-10'


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-tasks',
    version='0.1',
    packages=['tasks'],
    include_package_data=True,
    license='BSD License',  # example license
    description='A simple Django app to conduct Web-based tasks.',
    long_description=README,
    url='http://www.example.com/',
    author='ZSR',
    author_email='lynn_0401@163.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',
    ],
)

$$ python setup.py sdist
running sdist
running egg_info
writing django_tasks.egg-info/PKG-INFO
writing top-level names to django_tasks.egg-info/top_level.txt
writing dependency_links to django_tasks.egg-info/dependency_links.txt
reading manifest file 'django_tasks.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'LICENSE'
warning: no files found matching '*' under directory 'docs'
writing manifest file 'django_tasks.egg-info/SOURCES.txt'
running check
creating django-tasks-0.1
creating django-tasks-0.1/django_tasks.egg-info
creating django-tasks-0.1/tasks
creating django-tasks-0.1/tasks/static
creating django-tasks-0.1/tasks/static/tasks
creating django-tasks-0.1/tasks/templates
creating django-tasks-0.1/tasks/templates/tasks
copying files to django-tasks-0.1...
copying MANIFEST.in -> django-tasks-0.1
copying README.rst -> django-tasks-0.1
copying setup.py -> django-tasks-0.1
copying django_tasks.egg-info/PKG-INFO -> django-tasks-0.1/django_tasks.egg-info
copying django_tasks.egg-info/SOURCES.txt -> django-tasks-0.1/django_tasks.egg-info
copying django_tasks.egg-info/dependency_links.txt -> django-tasks-0.1/django_tasks.egg-info
copying django_tasks.egg-info/top_level.txt -> django-tasks-0.1/django_tasks.egg-info
copying tasks/__init__.py -> django-tasks-0.1/tasks
copying tasks/admin.py -> django-tasks-0.1/tasks
copying tasks/apps.py -> django-tasks-0.1/tasks
copying tasks/models.py -> django-tasks-0.1/tasks
copying tasks/tests.py -> django-tasks-0.1/tasks
copying tasks/urls.py -> django-tasks-0.1/tasks
copying tasks/views.py -> django-tasks-0.1/tasks
copying tasks/static/tasks/style.css -> django-tasks-0.1/tasks/static/tasks
copying tasks/templates/tasks/detail.html -> django-tasks-0.1/tasks/templates/tasks
copying tasks/templates/tasks/index.html -> django-tasks-0.1/tasks/templates/tasks
copying tasks/templates/tasks/results.html -> django-tasks-0.1/tasks/templates/tasks
Writing django-tasks-0.1/setup.cfg
Creating tar archive
removing 'django-tasks-0.1' (and everything under it)

  

使用自己的包

 $pip install --user django-tasks/dist/django-tasks-0.1.tar.gz 
Unpacking ./django-tasks/dist/django-tasks-0.1.tar.gz
  Running setup.py (path:/tmp/pip-y8B6hl-build/setup.py) egg_info for package from file:///home/ly/PycharmProjects/ltest/django-tasks/dist/django-tasks-0.1.tar.gz
    warning: no files found matching 'LICENSE'
    warning: no files found matching '*' under directory 'docs'
  Requirement already satisfied (use --upgrade to upgrade): django-tasks==0.1 from file:///home/ly/PycharmProjects/ltest/django-tasks/dist/django-tasks-0.1.tar.gz in /home/ly/.local/lib/python2.7/site-packages
Cleaning up...

  

pip uninstall django-polls
原文地址:https://www.cnblogs.com/zsr0401/p/6387102.html