Django developers, in this month’s Python column, we’ll introduce packages that can help you. These packages are our favorite Django libraries, saving development time, reducing boilerplate code, and generally making our lives easier. We have six packages for Django applications and two packages for Django’s REST framework. In almost all of our projects, we use these bags, really, I’m not kidding.
But before you read on, check out our tips on making the Django admin background more secure, as well as this article about the five most popular open source Django packages.
A useful and time-saving collection of tools: Django-extensions
Django-extensions this popular Django package is full of useful tools, such as the following admin commands:
-
Shell_plus opens Django’s admin shell, which automatically imports all database models. When testing complex data relationships, there is no need to import from several different applications.
-
Clean_pyc deletes.pyc files at all locations in the project directory
-
Create_template_tags Creates a directory structure for template tags in the specified application.
-
Describe_form outputs a form definition of the model that can be pasted into forms.py. (Note that this method creates regular Django forms, not model forms.)
-
Notes outputs all comments in your project with TODO, FIXME, etc.
Django-extensions also includes several useful abstract base classes that satisfy common patterns when defining models. You can inherit these base classes when you need the following models:
-
TimeStampedModel: The base class of this model contains the Created and Modified fields, as well as a save() method that automatically updates the created and Modified values in appropriate scenarios.
-
ActivatorModel: Use this base class if your model requires fields like status, Activate_date, and deactivate_date. It also comes with a manager that enables.active() and.inactive() query sets.
-
TitleDescriptionModel and TitleSlugDescriptionModel: The two models include the title and description fields, where the Description field also includes the SLUG, which is automatically generated based on the title field.
Django-extensions has many more features that might be useful for your project, so take a look at its documentation.
12 Factor application configuration: Django-environ
Django-environ provides a 12-factor application methodology for configuring Django projects. It is a collection of other libraries, including Envparse and Honcho. Once django-environ is installed, create an.env file in the root directory of your project and use this file to define variables that vary from environment to environment or that need to be kept private. (such as API key, whether debugging is enabled, database URL, etc.)
Then, import environ in your project’s settings.py and set environ.path () and environ.env () using the examples in the official documentation. Env (‘VARIABLE_NAME’) to get the values defined in the.env file.
Create an excellent admin command: Django-click
Django-click is based on click. Click twice), which is useful for writing Django administrative commands. The library doesn’t have a lot of documentation, but the code repository has a directory for test commands that is very useful. The basic Hello World command for django-click reads like this:
# app_name.management.commands.hello.py
import djclick as click
@click.command()
@click.argument(‘name’)
def command(name):
click.secho(f’Hello, {name}’)
Call it from the command line and do this:
>> ./manage.py hello Lacey
Hello, Lacey
Handling finite state machines: Django-fsm
Django-fsm adds finite state machine support to Djangos models. If you manage a news site and want to use states like “writing,” “Editing,” and “published,” Django-FSM can define those states for you, as well as manage the rules and restrictions for state changes.
Django-fsm provides the FSMField field for the model, which defines the state of the model instance. With the django-FSM @transition modifier, you can define methods for state changes and handle any side effects of state changes.
Although the Django-FSM documentation is lightweight, this GitHub Gist provides a good introduction to finite state machines and Django-FSM.
Contact form: #django-contact-form
A contact form is standard on a website. But don’t write all the boilerplate code yourself. Use Django-contact-form in a matter of minutes. It comes with an optional spam-filtering form class (as well as an unfiltered regular form class) and a ContactFormView base class whose methods can be overridden or customized. It also guides you through the creation of templates to make the form work.
User registration and authentication: Django-Allauth
Django-allauth is a Django application that provides views, forms, and urls for user registration, login/logout, password reset, and third-party user authentication (such as GitHub or Twitter). It supports email address authentication as a user name, and is well documented. The configuration can be a bit confusing when you first use it; Read the installation instructions carefully and focus on customizing your configuration to make sure that all the configurations that enable a feature are used correctly.
User authentication for handling the Django REST framework: Django-rest-auth
If your Django development involves providing apis externally, you’re probably using the Django REST Framework (DRF). If you’re using DRF, you should try django-rest-Auth, which provides endpoints for user registration, login/logout, password reset, and social media authentication (done by adding support for Django-Allauth, the two packages work well together).
Django-rest-swagger API Visualization of Django REST framework: Django-rest-Swagger
Django REST Swagger provides a feature-rich user interface for interacting with the apis of the Django REST framework. Just install Django REST Swagger, add it to the installed application of your Django project, and add Swagger’s view and URL pattern to urls.py. The API docString takes care of the REST.
The API’s user interface shows all endpoints and available methods in app dimensions, lists the operations available to those endpoints, and it provides functionality to interact with the API (such as adding/removing/getting records). Django-rest-swagger generates documentation for each endpoint from docstrings in the API view. By doing so, you create an API documentation for your project, which is useful to you, front-end developers, and users.