Python package for django

Advanced tutorial: How to write reusable 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–7, we encourage you to review these so that your example project matches the one described below.

Reusability matters¶

It’s a lot of work to design, build, test and maintain a web application. Many Python and Django projects share common problems. Wouldn’t it be great if we could save some of this repeated work?

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.

A Python package provides a way of grouping related Python code for easy reuse. A package contains one or more files of Python code (also known as “modules”).

Читайте также:  Тег А

A package can be imported with import foo.bar or from foo import bar . For a directory (like polls ) to form a package, it must contain a special file __init__.py , even if this file is empty.

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.

Later on we use the term packaging to describe the process of making a Python package easy for others to install. It can be a little confusing, we know.

Your project and your reusable app¶

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

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.gif style.css templates/ polls/ detail.html index.html results.html tests.py urls.py views.py templates/ admin/ base_site.html

You created mysite/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.

The polls directory could now be copied into a new Django project and immediately reused. It’s not quite ready to be published though. For that, we need to package the app to make it easy for others to install.

Installing some prerequisites¶

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.

Packaging your app¶

Python packaging refers to preparing your app in a specific format that can be easily installed and used. Django itself is packaged very much like this. For a small app like polls, this process isn’t too difficult.

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

Читайте также:  Python удалить перевод строки из строки

Choosing a name for your app When choosing a name for your package, check resources like PyPI to avoid naming conflicts with existing packages. It’s often useful to prepend django- to your module name when creating a package to distribute. This helps others looking for Django apps identify your app as Django specific. Application labels (that is, the final part of the dotted path to application packages) must be unique in INSTALLED_APPS . Avoid using the same label as any of the Django contrib packages , for example auth , admin , or messages .

===== Polls ===== 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 = [ . "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.
[build-system] requires = ['setuptools>=40.8.0'] build-backend = 'setuptools.build_meta' 
[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.8 Programming Language :: Python :: 3.9 Topic :: Internet :: WWW/HTTP Topic :: Internet :: WWW/HTTP :: Dynamic Content [options] include_package_data = true packages = find: python_requires = >=3.8 install_requires = Django >= X.Y # Replace "X.Y" as appropriate 
from setuptools import setup setup() 

Источник

Django Packages is a directory of reusable apps, sites, tools, and more for your Django projects.

Small components used to build projects. An app is anything that is installed by placing in settings.INSTALLED_APPS.

Читайте также:  Map сортировка по значениям java

Frameworks

Large efforts that combine many python modules or apps. Examples include Django, django-cms, and Mezzanine. Most CMSes fall into this category, and so do storefronts.

Projects

This is for individual projects such as Django Packages, DjangoProject.com, and others.

Other

Other are not installed by settings.INSTALLED_APPS, are not frameworks or sites but still help Django in some way.

Latest Python 3 Package Releases

django-flyio v0.1.1

A set of simple utilities for Django apps running on Fly.io.

faker-file v0.17.2

Create files with fake data. In many formats. With no efforts.

django-payments-flow v0.1.4

Conector Flow Chile para Django Payments

faker-file v0.17.1

Create files with fake data. In many formats. With no efforts.

django-admin-commands2 v1.1.3

Tool to execute management commands from admin with ease and control.

Latest Five Packages Added

django-payments-khipu v0.1.2

Conector Khipu para Django Payments

django-payments-flow v0.1.4

Conector Flow Chile para Django Payments

django-auto-one-to-one v3.3.5

Automatically create and delete «hanging» OneToOne model instances

django-bootstrap5 v23.3

django-flyio v0.1.1

A set of simple utilities for Django apps running on Fly.io.

Random Five Packages

Anymail v10.0

Django email backends and webhooks for Amazon SES, Brevo (Sendinblue), MailerSend, Mailgun, Mailjet, Postmark, Postal, SendGrid, SparkPost and more

Django JET v1.0.8

Modern responsive template for the Django admin interface with improved functionality. We are proud to announce completely new Jet. Please check out Live Demo

django-form-scaffold v1.1.0

Utility for generating dynamic Django template representations of the inbuilt form.as_p/as_ul/as_table output.

django-url-shortener v0.0.1

A Django URL shortening application, with the ability to create custom short URLs

django EL(Endless) pagination v4.0.0

Django endless pagination tools. Ajax, multiple and lazy pagination, Twitter-style and Digg-style pagination.

Search Weight Package Description Last PyPI release: Repo Forks Stars
>% > Grid: > > N/A > N/A > N/A

Projects listed on Django Packages are third-party packages. They are not vetted nor endorsed by the Django Software Foundation. Use them at your own risk.

© 2010-2023 Contributors, 2010-2021 funded by Two Scoops Press, an imprint of Feldroy.
Originally developed by Daniel Roy Greenfeld & Audrey Roy Greenfeld.
Currently maintained by Jeff Triplett and development sponsored by REVSYS.
Hosted on DigitalOcean. Please use our free $200 credit referral link to get you started.

Источник

Оцените статью