重用性对于我们的app非常重要,能够避免重复工作。本课程将教你如何去重用你的app

参考文档为https://docs.djangoproject.com/en/1.11/intro/reusable-apps/,结合项目实际在一步步演示。

创建你的重用项目

我们从头开始创建一个应用codingsohodemo用于演示

首先创建一个项目mydemo

django-admin.py startproject demo

接着创建我们打算重用的应用codingsohodemo

python manage.py startapp codingsohodemo

在这个app实现你的功能,此处省略。

最终我们可以看到如下的结构

├─codingsohodemo
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ urls.py
│ │ views.py
│ │ __init__.py
│ │
│ ├─migrations
│ │ __init__.py
│ │
│ ├─static
│ │ ├─codingsohodemo
│ │ │ ├─css
│ │ │ │ pgwslideshow.min.css
│ │ │ │
│ │ │ └─js
│ │ │ pgwslideshow.min.js
│ │ │
│ │ └─js
│ │ jquery.min.js
│ │
│ └─templates
│ │ base_site.html
│ │
│ └─codingsohodemo
│ pgwshow.html

上面的项目中,我们给app创建了独立的template和static目录,这样它比较容易发布,加入新项目时安装会比较容易,避免过多的逻辑交织。

codingsohodemo目录可以直接拷贝到新的项目中立即重用。如果想让别人也能够安装的话,我们需要发布它。

安装前置条件

我们会用setuptools去编译我们的包,这是目前推荐的打包工具。后面我们可以通过pip来安装或卸载这个包。

打包应用

流程并不复杂,一步一步按照下面的步骤做就可以了

1.在你的项目外创建父目录。这个目录名后面会成为包的名字,要先检查一下这个名字是否已存在,避免和现有包冲突。通常我们可以在前面加个django-,这样便于别人搜索django项目。

2.把codingsohodemo这个目录拷贝到django-codingsohodemo

3.创建django-codingsohodemo/README.rst, 内容如下,可以根据实际情况修改

=====
django-codingsohodemo
=====

django-codingsohodmeo project is demo application for codingsoho

Detailed documentation is in the "docs" directory.

Quick start
-----------

1. Add "codingsohodemo" to your INSTALLED_APPS setting like this::

    INSTALLED_APPS = [
        ...
        'codingsohodemo',
    ]

2. Include the polls URLconf in your project urls.py like this::

    url(r'^codingsohodemo/', include('codingsohodemo.urls')),

3. Run `python manage.py migrate` to create the codingsohodemo models.

4. Start the development server and visit http://127.0.0.1:8000/admin/
   to create a models if needed (you'll need the Admin app enabled).

5. Visit http://127.0.0.1:8000/codingsohodemo/ to participate in the poll.

4.创建django-codingsohodemo/LICENSE,这个不是本文的范围。你根据你的实际情况完成你的license,很多django项目用的是BSD License。我只是参考某个项目写了MIT License

The MIT License (MIT)

Copyright (c) 2018 CodingSoho

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

5.创建django-codingsohodemo/setup.py, 详情可参考setuptools docs ,下面是我的例子

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-codingsohodemo',
    version='1.1',
    packages=find_packages(),
    include_package_data=True,
    license='BSD License', # example license
    description='django-codingsohodmeo project is demo application for codingsoho',
    long_description=README,
    url='http://www.codingsoho.com/',
    author='Horde Chief',
    author_email='hordechief@qq.com',
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Framework :: Django :: 1.11', # 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 :: 2',
        'Programming Language :: Python :: 2.7',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)

根据你的情况修改name,versiondescription,url,author,author_email.

在classifiers里面,更新Django的版本和需要的python版本。

6.默认情况下,只有python文件和包会被打包。如果想添加额外的文件,需要新建另外的文件django-codingsohodemo/MANIFEST.in

include LICENSE
include README.rst
recursive-include codingsohodemo/static *
recursive-include codingsohodemo/templates *

7.可选步骤,如果你想额外的创建文档的话,也可以在这儿加进去。注意,里面必须放一些文件,否则空文件夹不会被打包。

recursive-include docs *

8.最后,我们可以用下面命令进行打包。在django-codingsohodemo目录下执行。这样它会创建一个dist目录,并且在里面生成包文件django-codingsohodemo-1.0.tar.gz。

python setup.py sdist

发布新的版本时记得修改版本号,这样它会创建新的打包文件,例如,如果我们修改版本为1.1,新的文件为 django-codingsohodemo-1.1.tar.gz

从你自己的包安装

前面我们把codingsohodemo目录移出去了,现在我们可以pip安装让它继续工作。

执行pip install dist\django-codingsohodemo-1.0.tar.gz即可完成安装。

你可以通过pip uninstall django-codingsohodemo卸载

发布应用

应用的发出参考python官网资料 https://packaging.python.org/tutorials/packaging-projects/#uploading-your-project-to-pypi

首先你要有一个pypi账号,登陆https://test.pypi.org/account/register/注册

注册成功后,你就可以上传发布包了。

安装twine

pip install twine

执行下面命令完成上传

twine upload --repository-url https://test.pypi.org/legacy/ dist/*

中间会提示你输入用户名和密码验证

上传成功后访问https://test.pypi.org/project/django-codingsohodemo/检查是否成功

至此,我们可以执行pip install djagno-codingsohodemo来进行安装了。

发布到pypi

注意,以上的步骤是让你发布到Test Pypi,这个只是暂时的。你需要把你的项目真正发布到Pypi。

首先注册地址不同,为https://pypi.org

其次上传命令不一样

twine upload dist/*

Uploading distributions to https://upload.pypi.org/legacy/

完成之后, 你可以使用pip install django-codingsohodemo来安装你的包了

注意:上传成功后不能立即安装,它需要一点时间同步。上传时最好把老版本删除,否则可能会有报错

参考资料

评论

codingsoho: 5 years, 8 months ago
新评论系统完成,测试一下 !!!!!
codingsoho: 5 years, 8 months ago
@bhe001 good!!!

留言请先登录注册! 并在激活账号后留言!