Οδηγός για προχωρημένους: Πως να γράψετε επαναχρησιμοποιήσιμα appsLink to this heading

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.

Αν δεν έχετε ολοκληρώσει πρόσφατα τον Οδηγό του Django (μέρη 1 εως 7), σας ενθαρρύνουμε να ρίξετε μια ματιά (ή ακόμη και να τον ακολουθήσετε πλήρως) ούτως ώστε το project σας να συμβαδίζει με αυτό που θα ακολουθήσει.

Η επαναχρησιμοποίηση έχει σημασίαLink to this heading

Απαιτεί πολύ δουλειά για να σχεδιάσετε, χτίσετε, τεστάρετε και διατηρήσετε μια 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.

Το project σας και η επαναχρησιμοποιήσιμη appLink to this heading

After the previous tutorials, our project should look like this:

Text
mysite/
    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

Δημιουργήσατε τον φάκελο mysite/templates στον Οδηγό 7 και τον φάκελο polls/templates στον Οδηγό 3. Τώρα ίσως φαίνεται πιο καθαρά ο λόγος που διαλέξαμε να έχουμε ξεχωριστούς template φακέλους για το project και για το application: οτιδήποτε είναι μέρος της εφαρμογής polls βρίσκεται μέσα στο polls. Αυτό κάνει την εφαρμογή ανεξάρτητη και ευκολότερη να ενσωματωθεί μέσα σε ένα καινούργιο project.

Ο φάκελο polls μπορεί εύκολα να αντιγραφεί σε ένα καινούργιο Django project και αμέσως να επαναχρησιμοποιηθεί. Δεν είναι, όμως, ακόμη έτοιμο να διανεμηθεί (εκδοθεί). Γι’ αυτό θα πρέπει να μετατρέψουμε την εφαρμογή μας σε ένα package για να διευκολύνουμε τους άλλους που θα την εγκαταστήσουν.

Εγκαθιστώντας μερικά προαπαιτούμεναLink to this heading

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 σαςLink to this heading

Το Python packaging αναφέρεται στην προετοιμασία του app σε μια συγκεκριμένη μορφή ούτως ώστε να μπορεί να εγκατασταθεί και να χρησιμοποιηθεί εύκολα. Το Django από μόνο του έχει πακεταριστεί με παρόμοιο τρόπο. Για μικρά apps όπως το polls, αυτή η διαδικασία δεν είναι δύσκολη.

  1. First, create a parent directory for the package, outside of your Django project. Call this directory django-polls.

  2. Move the polls directory into django-polls directory, and rename it to django_polls.

  3. Edit django_polls/apps.py so that name refers to the new module name and add label to give a short name for the app:

    django-polls/django_polls/apps.py
    Python
    from django.apps import AppConfig
    
    
    class PollsConfig(AppConfig):
        default_auto_field = "django.db.models.BigAutoField"
        name = "django_polls"
        label = "polls"
    
  4. Δημιουργήστε ένα αρχείο django-polls/README.rst με το ακόλουθο περιεχόμενο:

    django-polls/README.rst
    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.
    
  5. Δημιουργήστε ένα αρχείο django-polls/LICENSE. Η επιλογή μιας άδειας (license) είναι πέρα από τους σκοπούς αυτού του οδηγού, αλλά αρκεί να πούμε ότι κώδικας που έχει δημοσιευτεί χωρίς άδεια είναι άχρηστος. Το Django και πολλά άλλα συμβατα με το Django apps είναι διανεμημένα κάτω από την BSD άδεια. Ωστόσο, είστε ελεύθεροι να διαλέξετε μια της αρέσκειας σας. Κρατήστε στο μυαλό σας, όμως, ότι η επιλογή της αδείας επηρεάζει το ποιος θα μπορεί να χρησιμοποιήσει τον κώδικα σας.

  6. Next we’ll create pyproject.toml, setup.cfg, and setup.py files which detail how to build and install the app. A full explanation of these files is beyond the scope of this tutorial, but the setuptools documentation has a good explanation. Create the django-polls/pyproject.toml, django-polls/setup.cfg, and django-polls/setup.py files with the following contents:

    django-polls/pyproject.toml
    Toml
    [build-system]
    requires = ['setuptools>=40.8.0']
    build-backend = 'setuptools.build_meta'
    
    django-polls/setup.cfg
    Ini
    [metadata]
    name = django-polls
    version = 0.1
    description = A Django app to conduct web-based polls.
    long_description = file: README.rst
    url = https://www.example.com/
    author = Your Name
    author_email = yourname@example.com
    license = BSD-3-Clause  # Example license
    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
        Topic :: Internet :: WWW/HTTP
        Topic :: Internet :: WWW/HTTP :: Dynamic Content
    
    [options]
    include_package_data = true
    packages = find:
    python_requires = >=3.10
    install_requires =
        Django >= X.Y  # Replace "X.Y" as appropriate
    
    django-polls/setup.py
    Python
    from setuptools import setup
    
    setup()
    
  7. Only Python modules and packages are included in the package by default. To include additional files, we’ll need to create a MANIFEST.in file. The setuptools docs referred to in the previous step discuss this file in more detail. To include the templates, the README.rst and our LICENSE file, create a file django-polls/MANIFEST.in with the following contents:

    django-polls/MANIFEST.in
    Text
    include LICENSE
    include README.rst
    recursive-include django_polls/static *
    recursive-include django_polls/templates *
    
  8. It’s optional, but recommended, to include detailed documentation with your app. Create an empty directory django-polls/docs for future documentation. Add an additional line to django-polls/MANIFEST.in:

    Text
    recursive-include docs *
    

    Σημειώστε ότι ο φάκελος docs δεν θα συμπεριληφθεί στο package σας εκτός και αν προσθέσετε μερικά αρχεία μέσα. Σε περίπτωση που δεν το γνωρίζατε, πολλά Django apps παρέχουν το δικό τους documentation και online μέσα από ιστοσελίδες όπως η readthedocs.org.

  9. Try building your package by running python setup.py sdist inside django-polls. This creates a directory called dist and builds your new package, django-polls-0.1.tar.gz.

For more information on packaging, see Python’s Tutorial on Packaging and Distributing Projects.

Χρησιμοποιώντας το δικό σας packageLink to this heading

Από τη στιγμή που μετακινήσαμε τον φάκελο polls έξω από το project, η εφαρμογή μας δεν δουλεύει πλέον, κάτι το οποίο είναι λογικό. Σε αυτή την παράγραφο θα εγκαταστήσουμε την εφαρμογή μας,``django-polls``, σαν ένα (τρίτο) πακέτο.

  1. To install the package, use pip (you already installed it, right?):

    Shell
    python -m pip install --user django-polls/dist/django-polls-0.1.tar.gz
    
  2. Update mysite/settings.py to point to the new module name:

    Code
    INSTALLED_APPS = [
        "django_polls.apps.PollsConfig",
        ...,
    ]
    
  3. Update mysite/urls.py to point to the new module name:

    Code
    urlpatterns = [
        path("polls/", include("django_polls.urls")),
        ...,
    ]
    
  4. Run the development server to confirm the project continues to work.

Δημοσιεύοντας το app σαςLink to this heading

Τώρα που έχουμε μετατρέψει σε package και τεστάρει την εφαρμογή django-polls, είναι έτοιμη να διαμοιραστεί στον υπόλοιπο κόσμο! Αν αυτό δεν ήταν κάποιο παράδειγμα, θα μπορούσατε να:

Installing Python packages with a virtual environmentLink to this heading

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.