版权声明:本文为博主原创文章,转载请指明源地址! https://blog.csdn.net/qq_32662595/article/details/85335347
一、 使用URLConf
urlpatterns = [path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ]
二、打包成tar,使用pip安装
具体操作如下:(以前面的polls为例)
- 新建polls 的父目录 django-polls(名字自取)
- 将polls移动到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::
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.
- 创建
LICENSE
许可证文件。Django和许多Django-compatible应用程序分布在BSD许可下;然而,你自由选择自己的许可。你的许可选择将影响是谁能够使用你的代码。 - 创建
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',
],
)
- 创建
MANIFEST.in
文件,包括模板、README。rst和我们的许可文件,创建一个文件django-polls /清单.
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
- 该项可选。将下列加入
MANIFEST.in
:文件中
recursive-include docs *
- 执行以下命令,将会生成在
django-polls/dist/django-polls-0.1.tar.gz
包
python setup.py sdist
- 使用该包的方法。跟安装其他包一样,使用指定用户安装
pip install --user django-polls/dist/django-polls-0.1.tar.gz
- 卸载已按照的包
pip uninstall django-polls
- 然后在manage.py下执行命令,在浏览器访问
http://127.0.0.1:8000/admin/
即可
python manage.py runserver
- 项目的总体目录如下(已卸载安装的
django-polls
)
- 你可将tar发给任何人。