Tutorial avançado: Como escrever aplicações reutilizáveis¶
This advanced tutorial begins where Tutorial 8 left off. We’ll be turning our web-poll into a standalone Python package you can reuse in new projects and share with other people.
If you haven’t recently completed Tutorials 1–8, we encourage you to review these so that your example project matches the one described below.
Reutilização importa¶
É um grande trabalho projetar, contruir, testar e manter uma aplicação web. Muitos projetos Python e Django compartilham problemas em comum. Não seria ótimo se nós pudessemos economizar um pouco desse trabalho repetido?
Reusability is the way of life in Python. The Python Package Index (PyPI) has a vast range of packages you can use in your own Python programs. Check out Django Packages for existing reusable apps you could incorporate in your project. Django itself is also a normal Python package. This means that you can take existing Python packages or Django apps and compose them into your own web project. You only need to write the parts that make your project unique.
Vamos dizer que você está iniciando um projeto que precisa de uma aplicação de enquetes como a que estivemos trabalhando. Como fazer essa aplicação reutilizável? Sua sorte é que é que você já está no caminho. Em Tutorial, vimos como podemos desacoplar a enquete do URLconf do projeto usando um include
. Neste tutorial, iremos adiante para tornar a aplicação fácil de usar em novos projetos e pronta para publicar para outros instalarem e usarem.
Pacote? Aplicação?
Um pacote Python fornece uma maneira de agrupar códigos Python relacionados para fácil reutilização. Um pacote contém um ou mais arquivos de códigos Python (também conhecidos como “módulos”).
Um pacote pode ser importado com import foo.bar
ou from foo import bar
. Para um diretório (como polls
) formar um pacote, ele deve conter um arquivo especial __init__.py
, mesmo se este arquivo estiver vazio.
Uma aplicação Django é um pacote Python especificamente destinado para usar em um projeto Django. Uma aplicação pode usar convenções comuns do Django, tais como ter submódulos models
, tests
, urls
, e views
.
Posteriormente usamos o termo empacotamento para descrever o processo de tornar um pacote Python fácil para outros instalarem. Pode ser um pouco confuso, nós sabemos.
Seu projeto e sua aplicação reutilizável¶
After the previous tutorials, our project should look like this:
djangotutorial/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
0001_initial.py
models.py
static/
polls/
images/
background.png
style.css
templates/
polls/
detail.html
index.html
results.html
tests.py
urls.py
views.py
templates/
admin/
base_site.html
You created djangotutorial/templates
in Tutorial 7, and polls/templates
in
Tutorial 3. Now perhaps it is clearer why we chose
to have separate template directories for the project and application:
everything that is part of the polls application is in polls
. It makes the
application self-contained and easier to drop into a new project.
O diretório polls
poderia agora ser copiado em um novo projeto Django, e imediatamente reusada. Embora não esteja já pronta para ser publicada. Para isso, nós precisamos empacotar a aplicação para tornar fácil para outros instalarem.
Instalando alguns pré-requisitos¶
The current state of Python packaging is a bit muddled with various tools. For
this tutorial, we’re going to use setuptools to build our package. It’s
the recommended packaging tool (merged with the distribute
fork). We’ll
also be using pip to install and uninstall it. You should install these
two packages now. If you need help, you can refer to how to install
Django with pip. You can install setuptools
the same way.
Empacotando sua aplicação¶
Empacotamento Python refere-se a preparar sua aplicação num formato específico que possa ser facilmente instalado e usado. O próprio Django é empacotado de uma maneira muito parecida. Para aplicações pequenas, como a de enquetes, esse processo não é tão difícil.
First, create a parent directory for the package, outside of your Django project. Call this directory
django-polls
.Escolhendo um nome para a sua aplicação
When choosing a name for your package, check PyPI to avoid naming conflicts with existing packages. We recommend using a
django-
prefix for package names, to identify your package as specific to Django, and a correspondingdjango_
prefix for your module name. For example, thedjango-ratelimit
package contains thedjango_ratelimit
module.Rótulo da aplicação (isto é, a parte final do caminho pontilhado para os pacotes da aplicação) deve ser único no
INSTALLED_APPS
. Evite usar o mesmo rótulo como os do pacote contrib do Django, por exemploauth
,admin
, oumessages
.Move the
polls
directory intodjango-polls
directory, and rename it todjango_polls
.Edit
django_polls/apps.py
so thatname
refers to the new module name and addlabel
to give a short name for the app:django-polls/django_polls/apps.py
¶from django.apps import AppConfig class PollsConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "django_polls" label = "polls"
Crie um arquivo
django-polls/README.rst
com o seguinte conteúdo:django-polls/README.rst
¶============ django-polls ============ django-polls is a 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 = [ ..., "django_polls", ] 2. Include the polls URLconf in your project urls.py like this:: path("polls/", include("django_polls.urls")), 3. Run ``python manage.py migrate`` to create the models. 4. Start the development server and visit the admin to create a poll. 5. Visit the ``/polls/`` URL to participate in the poll.
Crie um arquivo
django-polls/LICENSE
. Escolher uma licença está fora do escopo deste tutorial, mas é suficiente dizer que código distribuído publicamente sem uma licença é inútil. Djando e muitas aplicações compatíveis com o Django são distribuídas sob a licença BSD; no entanto, você é livre para escolher sua própria licença. Apenas esteja ciente que a escolha da sua licença afetará quem pode usar seu código.Next we’ll create the
pyproject.toml
file which details how to build and install the app. A full explanation of this file is beyond the scope of this tutorial, but the Python Packaging User Guide has a good explanation. Create thedjango-polls/pyproject.toml
file with the following contents:django-polls/pyproject.toml
¶[build-system] requires = ["setuptools>=69.3"] build-backend = "setuptools.build_meta" [project] name = "django-polls" version = "0.1" dependencies = [ "django>=X.Y", # Replace "X.Y" as appropriate ] description = "A Django app to conduct web-based polls." readme = "README.rst" requires-python = ">= 3.10" authors = [ {name = "Your Name", 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", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "Topic :: Internet :: WWW/HTTP", "Topic :: Internet :: WWW/HTTP :: Dynamic Content", ] [project.urls] Homepage = "https://www.example.com/"
Many common files and Python modules and packages are included in the package by default. To include additional files, we’ll need to create a
MANIFEST.in
file. To include the templates and static files, create a filedjango-polls/MANIFEST.in
with the following contents:django-polls/MANIFEST.in
¶recursive-include django_polls/static * recursive-include django_polls/templates *
It’s optional, but recommended, to include detailed documentation with your app. Create an empty directory
django-polls/docs
for future documentation.Note que o diretório
docs
não será incluido no seu pacote a menos que você adicione alguns arquivos para ele. Muitas aplicações Django fornecem sua documentação online através de sites comoMany Python projects, including Django and Python itself, use Sphinx to build their documentation. If you choose to use Sphinx you can link back to the Django documentation by configuring Intersphinx and including a value for Django in your project’s
intersphinx_mapping
value:intersphinx_mapping = { # ... "django": ( "https://docs.djangoproject.com/en/stable/", None, ), }
With that in place, you can then cross-link to specific entries, in the same way as in the Django docs, such as “
:attr:`django.test.TransactionTestCase.databases`
”.Check that the build package is installed (
python -m pip install build
) and try building your package by runningpython -m build
insidedjango-polls
. This creates a directory calleddist
and builds your new package into source and binary formats,django_polls-0.1.tar.gz
anddjango_polls-0.1-py3-none-any.whl
.
Para mais informações sobre empacotamente, veja o Tutorial sobre Empacotamento e Distribuição de Projetos.
Utilizando o seu próprio pacote¶
Uma vez que movemos o diretório polls
para fora do projeto, ele não funciona mais. Vamos consertar isso agora ao instalar nosso novo pacote django-polls
.
Instalando uma biblioteca de usuário
Os passos a seguir instalam django-polls
como uma biblioteca do usuário. Instalação por usuário tem uma série de vantagens sobre a instalação do pacote em todo o sistema, tais como ser utilizável em sistemas onde você não tem acesso de administrador, bem como prevenir o pacote de afetar serviços do sistema e outros usuários da máquina.
Note that per-user installations can still affect the behavior of system tools that run as that user, so using a virtual environment is a more robust solution (see below).
To install the package, use pip (you already installed it, right?):
python -m pip install --user django-polls/dist/django_polls-0.1.tar.gz
Update
mysite/settings.py
to point to the new module name:INSTALLED_APPS = [ "django_polls.apps.PollsConfig", ..., ]
Update
mysite/urls.py
to point to the new module name:urlpatterns = [ path("polls/", include("django_polls.urls")), ..., ]
Run the development server to confirm the project continues to work.
Publicando sua aplicação¶
Agora que nós empacotamos e testamos django-polls
, ele está pronto para ser compartilhado com o mundo! Se isso não foi apenas um exemplo, você pode agora:
Enviar o pacote por email para um amigo.
Enviar o pacote para seu site.
Colocar o pacote em um repositório público, tal como the Python Package Index (PyPI). packaging.python.org tem um bom tutorial para fazer isso.
Instalando pacotes Python com um ambiente virtual¶
Earlier, we installed django-polls
as a user library. This has some
disadvantages:
Modificar as bibliotecas de usuário pode afetar outros softwares em Python do seu sistema.
Você não poderá rodar versões diferentes do seu pacote (ou outros pacotes com o mesmo nome).
Typically, these situations only arise once you’re maintaining several Django projects. When they do, the best solution is to use venv. This tool allows you to maintain multiple isolated Python environments, each with its own copy of the libraries and package namespace.