Django apps 重用

版权声明:本文为博主原创文章,转载请指明源地址! https://blog.csdn.net/qq_32662595/article/details/85335347

一、 使用URLConf

urlpatterns = [path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ]

二、打包成tar,使用pip安装
具体操作如下:(以前面的polls为例)

  1. 新建polls 的父目录 django-polls(名字自取)
  2. 将polls移动到django-polls下
  3. 创建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::

    path('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.
  1. 创建LICENSE 许可证文件。Django和许多Django-compatible应用程序分布在BSD许可下;然而,你自由选择自己的许可。你的许可选择将影响是谁能够使用你的代码。
  2. 创建setup.py文件,该文件将详细提供如何创建和安装app,内容大致如下:
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-polls',
    version='0.1',
    packages=find_packages(),
    include_package_data=True,
    license='BSD License',  # example license
    description='A simple Django app to conduct Web-based polls.',
    long_description=README,
    url='https://www.example.com/',
    author='Your Name',
    author_email='[email protected]',
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Framework :: Django :: X.Y',  # replace "X.Y" as appropriate
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',  # example license
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)
  1. 创建MANIFEST.in文件,包括模板、README。rst和我们的许可文件,创建一个文件django-polls /清单.
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
  1. 该项可选。将下列加入MANIFEST.in:文件中
recursive-include docs *
  1. 执行以下命令,将会生成在 django-polls/dist/django-polls-0.1.tar.gz
python setup.py sdist
  1. 使用该包的方法。跟安装其他包一样,使用指定用户安装
pip install --user django-polls/dist/django-polls-0.1.tar.gz
  1. 卸载已按照的包
pip uninstall django-polls
  1. 然后在manage.py下执行命令,在浏览器访问http://127.0.0.1:8000/admin/即可
python manage.py runserver
  1. 项目的总体目录如下(已卸载安装的django-polls

在这里插入图片描述

  1. 你可将tar发给任何人。

猜你喜欢

转载自blog.csdn.net/qq_32662595/article/details/85335347
今日推荐