基於前面的教學,專案目錄應該如下:
mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    polls/
        __init__.py
        admin.py
        migrations/
            __init__.py
            0001_initial.py
        models.py
        static/
            polls/
                images/
                    background.gif
                style.css
        templates/
            polls/
                detail.html
                index.html
                results.html
        tests.py
        urls.py
        views.py
    templates/
        admin/
            base_site.html
目錄 polls 可被拷貝至新的 Django 工程,且可馬上重複使用。不過還不到發布的時候。我們需要打包這個應用,便於其他人安裝它。
目前,打包 Python 程式需要工具,有許多工具可以完成此項工作。在此教程中,我們將使用 setuptools 來打包我們的程式。這是推薦的打包工具(與 發布 分支合並)。我們仍舊使用 pip 來安裝和卸載這個工具。現在,你需要安裝這兩個包。如果你需要幫助,你可以參考 如何通過 pip 安裝 Django,你可以通過相同的方式安裝 setuptools。
Python 的 打包 將以一種特殊的格式組織你的應用,意在方便安裝和使用這個應用。Django 本身就被打包成類似的形式。對於一個小應用,例如 polls,這不會太難。
為你的應用選擇一個名字
當為你的包選一個名字時,避免使用像 PyPI 這樣已存在的包名,否則會導致沖突。當你創建你的發布包時,可以在模塊名前增加 django- 前綴,這是一個很常用也很有用的避免包名沖突的方法。同時也有助於他人在尋找 Django 應用時確認你的 app 是 Django 獨有的。
應用標簽(指用點分隔的包名的最後一部分)在 INSTALLED_APPS 中 必須 是獨一無二的。避免使用任何與 Django contrib packages 文檔中相同的標簽名,比如 auth,admin,messages。
將 polls 目錄移入 django-polls 目錄。
創建一個名為 django-polls/README.rst 的文件,包含以下內容:
#路徑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.
創建一個 django-polls/LICENSE 文件。選擇一個非本教程使用的授權協議,但是要足以說明發布代碼沒有授權證書是 不可能的 。Django 和很多相容 Django 的應用是以 BSD 授權協議發布的;不過,你可以自己選擇一個授權協議。只要確定你選擇的協議能夠限制未來會使用你的代碼的人。
下一步我們將創建 setup.py 用於說明如何構建和安裝應用的細節。關於此文件的完整介紹超出了此教程的範圍,但是 setuptools docs 有詳細的介紹。創建文件 django-polls/setup.py 包含以下內容:
#路徑django-polls/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()
# 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='yourname@example.com',
    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',
    ],
)
路徑django-polls/MANIFEST.in
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *