Django发布应用application

欢迎访问个人博客

Django deploy application

当Django的application开发完成之后,我们希望能将其作为模块分发出去供别人共享使用

How to write application

安装好Django之后

## create project
django-admin.py startproject mypro

## create app named `todo` (todo list)
cd mypro
python manage.py startapp todo

## configure app(todo) connect with project `mypro`
vim mypro/settings.py

    INSTALLED_APPS = [
        'django.contrib.admin',
         ...,
        'todo',
    ]

vim mypro/urls.py
    from django.conf.urls import url, include

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^todo/', include('todo.urls', namespace='todo')),
    ]

How to write setup.py

  • 创建一个父目录

    mkdir -pv /opt/django-todo

  • copy 刚才新增的 application todo 到 该目录下

    cp -r todo /opt/django-todo/

    一般开发完成application之后,在todo 下面会生成__pycache__migrations, 一般会保留migrations 作为应用的初始化; 或者删除不打包到Python package中,安装应用之后可以使用python manage.py makemigrations todo
    python manage.py migrate todo 来初始化;
    __pycache__ 可以删除不做打包

  • 新增README.MD 和 setup.py

    README.MD 作为项目的详细说明,这里省略,详细可以参考:todo README
    setup.py 是打包安装脚本,详细脚本如下:

import os
from setuptools import find_packages, setup

with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
    README = readme.read()

# 使 setup.py 能在任何地方运行
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name='colinws-todo',
    version='0.3',
    # packages=find_packages(),
    packages=['todo'],
    include_package_data=True,
    license='MIT License',
    description='A simple Django Todo List application',
    long_description=README,
    url='https://blog.colinspace.com/',
    author='Colin.Liu',
    author_email='colinservice@126.com',
    platforms = 'Linux,Unix',
    keywords = 'Colin,colinws,todo, todo list, django',
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Framework :: Django :: 1.11',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)
  • 编译打包、安装、注册、上传

    python setup.py sdist
    python setup.py register
    python setup.py upload

Python发布package到pypi配置

新增~/.pypirc 而不是 ~/.pypi

刚才报错Server response (410): Project pre-registration is no longer required or supported, so continue dire 就是配置文件命名成了 ~/.pypi

[distutils]
index-servers = pypi

[pypi]
repository: https://upload.pypi.org/legacy/
username: colin5063
password: xxxxxx

第三方安装

you can install via pip

pip install colinws-todo

or you can git clone this repository and use setup.py

git clone https://github.com/opscolin/Django-todo.git
cd Django-todo
python setup.py install

Quick start

  • Add "todo" to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [ ... 'todo',]
    
  • Include the todo URLconf in your project urls.pylike this:

    url(r'^todo/', include('todo.urls')),
    
  • Run below command to create todo models migrations.:

    python manage.py makemigrations todo
    
  • Run below command to create todo real models.:

    python manage.py migrate todo
    
  • Start the development server and visit http://127.0.0.1:8000/admin/ to create a todo (you’ll need the Admin app enabled).

  • Visit http://127.0.0.1:8000/todo/ to participate in the todo list.

Refer

    原文作者:全栈运维
    原文地址: https://www.jianshu.com/p/d52e4c6b5664
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞