Οδηγός για προχωρημένους: Πως να γράψετε επαναχρησιμοποιήσιμα apps¶
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.
Η επαναχρησιμοποίηση έχει σημασία¶
Απαιτεί πολύ δουλειά για να σχεδιάσετε, χτίσετε, τεστάρετε και διατηρήσετε μια web εφαρμογή (application). Πολλά Python και Django projects μοιράζονται κοινά προβλήματα. Δεν θα ήταν εξαιρετικά αν μπορούσαμε να γλυτώσουμε κάποιο μέρος από αυτή τη ρουτίνα;
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.
Let’s say you were starting a new project that needed a polls app like the one
we’ve been working on. How do you make this app reusable? Luckily, you’re well
on the way already. In Tutorial 1, we saw how we
could decouple polls from the project-level URLconf using an include
.
In this tutorial, we’ll take further steps to make the app easy to use in new
projects and ready to publish for others to install and use.
Package? App?
Ο όρος Python package παρέχει έναν τρόπο να ομαδοποιούμε Python κώδικα για εύκολη επαναχρησιμοποίηση. Ένα package περιέχει ένα ή περισσότερα αρχεία από Python κώδικα (επίσης γνωστά και ως «modules»).
Ένα package μπορεί να γίνει imported με τον κώδικα import foo.bar
ή from foo import bar
. Για να μετατραπεί ένας φάκελος (όπως ο φάκελος polls
) σε package, θα πρέπει να περιέχει ένα ειδικό αρχείο με το όνομα __init__.py
, ακόμη και αν αυτό το αρχείο είναι κενό.
A Django application is a Python package that is specifically intended
for use in a Django project. An application may use common Django
conventions, such as having models
, tests
, urls
, and views
submodules.
Αργότερα χρησιμοποιούμε τον όρο packaging για να περιγράψουμε την διαδικασία που χρειάζεται για να δημιουργηθεί ένα Python package. Μπορεί να σας μπερδεύει λιγάκι. Καταλαβαίνουμε!
Το project σας και η επαναχρησιμοποιήσιμη app¶
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.
Ο φάκελο polls
μπορεί εύκολα να αντιγραφεί σε ένα καινούργιο Django project και αμέσως να επαναχρησιμοποιηθεί. Δεν είναι, όμως, ακόμη έτοιμο να διανεμηθεί (εκδοθεί). Γι’ αυτό θα πρέπει να μετατρέψουμε την εφαρμογή μας σε ένα package για να διευκολύνουμε τους άλλους που θα την εγκαταστήσουν.
Εγκαθιστώντας μερικά προαπαιτούμενα¶
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.
Πακετάροντας το app σας¶
Το Python packaging αναφέρεται στην προετοιμασία του app σε μια συγκεκριμένη μορφή ούτως ώστε να μπορεί να εγκατασταθεί και να χρησιμοποιηθεί εύκολα. Το Django από μόνο του έχει πακεταριστεί με παρόμοιο τρόπο. Για μικρά apps όπως το polls, αυτή η διαδικασία δεν είναι δύσκολη.
First, create a parent directory for the package, outside of your Django project. Call this directory
django-polls
.Επιλέγοντας ένα όνομα για την εφαρμογή σας
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.Τα application labels (δηλαδή, η τελευταία λέξη των application packages στη διαδρομή με τις τελείες) πρέπει να είναι μοναδικό στη ρύθμιση
INSTALLED_APPS
. Αποφύγετε να χρησιμοποιείτε το ίδιο label με οποιοδήποτε από αυτά των Django contrib packages, όπως για παράδειγμαauth
,admin
ήmessages
.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"
Δημιουργήστε ένα αρχείο
django-polls/README.rst
με το ακόλουθο περιεχόμενο: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.
Δημιουργήστε ένα αρχείο
django-polls/LICENSE
. Η επιλογή μιας άδειας (license) είναι πέρα από τους σκοπούς αυτού του οδηγού, αλλά αρκεί να πούμε ότι κώδικας που έχει δημοσιευτεί χωρίς άδεια είναι άχρηστος. Το Django και πολλά άλλα συμβατα με το Django apps είναι διανεμημένα κάτω από την BSD άδεια. Ωστόσο, είστε ελεύθεροι να διαλέξετε μια της αρέσκειας σας. Κρατήστε στο μυαλό σας, όμως, ότι η επιλογή της αδείας επηρεάζει το ποιος θα μπορεί να χρησιμοποιήσει τον κώδικα σας.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.Σημειώστε ότι ο φάκελος
docs
δεν θα συμπεριληφθεί στο package σας εκτός και αν προσθέσετε μερικά αρχεία μέσα. Σε περίπτωση που δεν το γνωρίζατε, πολλά Django apps παρέχουν το δικό τους documentation και online μέσα από ιστοσελίδες όπως η readthedocs.org.Many 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
.
For more information on packaging, see Python’s Tutorial on Packaging and Distributing Projects.
Χρησιμοποιώντας το δικό σας package¶
Από τη στιγμή που μετακινήσαμε τον φάκελο polls
έξω από το project, η εφαρμογή μας δεν δουλεύει πλέον, κάτι το οποίο είναι λογικό. Σε αυτή την παράγραφο θα εγκαταστήσουμε την εφαρμογή μας,``django-polls``, σαν ένα (τρίτο) πακέτο.
Εγκατάσταση ως βιβλιοθήκη χρήστη (user library)
Τα ακόλουθα βήματα εγκαθιστούν το πακέτο django-polls
ως μια βιβλιοθήκη χρήστη. Αυτό σημαίνει ότι το πακέτο θα εγκατασταθεί στον υπολογιστή για τον συγκεκριμένο χρήστη και όχι γενικά (global) για όλους τους χρήστες του συστήματος. Αυτή η εγκατάσταση έχει πολλά πλεονεκτήματα έναντι της γενικής (global) διότι μπορεί η εφαρμογή να χρησιμοποιηθεί σε συστήματα όπου δεν υπάρχουν δικαιώματα διαχειριστή ή να αποτρέψει την πρόσβαση στην εφαρμογή από άλλους χρήστες ή άλλες υπηρεσίες του συστήματος.
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.
Δημοσιεύοντας το app σας¶
Τώρα που έχουμε μετατρέψει σε package και τεστάρει την εφαρμογή django-polls
, είναι έτοιμη να διαμοιραστεί στον υπόλοιπο κόσμο! Αν αυτό δεν ήταν κάποιο παράδειγμα, θα μπορούσατε να:
Στείλετε με email το package σε κάποιον φίλο.
Να ανεβάσετε το package στην ιστοσελίδα σας.
Post the package on a public repository, such as the Python Package Index (PyPI). packaging.python.org has a good tutorial for doing this.
Installing Python packages with a virtual environment¶
Earlier, we installed django-polls
as a user library. This has some
disadvantages:
Η αλλαγή των βιβλιοθηκών χρηστών μπορεί να επηρεάσει άλλο Python software μέσα στο σύστημα σας.
Δεν θα είστε σε θέση να τρέξετε πολλαπλές εκδόσεις το ίδιου πακέτου (ή άλλων με το ίδιο όνομα).
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.