Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the eight Python packages that simplify Django development?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Simplified Django development of the eight Python packages are which, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

This month's Python column will introduce some Django packages that are good for your work, as well as your personal or part-time projects.

Django developers, in this month's Python column, we will introduce some software packages that can help you. These packages are our favorite Django libraries that save development time and boilerplate code, which generally makes our lives easier. We have prepared six packages for the Django application and two packages for Django's REST framework. Almost all of our projects use these bags, really, no kidding.

But before you move on, take a look at our tips on making Django management background more secure, as well as this article on five open source Django packages.

A collection of useful and time-saving tools: django-extensions

Django-extensions, the Django package, is very popular and full of useful tools, such as the following administrative commands:

Shell_plus opens the management shell of Django, which has automatically imported all database models. When testing complex data relationships, you no longer need to import from several different applications.

Clean_pyc deletes .pyc files in all locations in the project directory

Create_template_tags creates the directory structure of template tags under the specified application.

The form definition of the describe_form output model can be pasted into the forms.py file. (it is important to note that this method creates a normal Django form, not a model form. )

Notes outputs all comments in your project marked 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 values of the created and modified fields in appropriate scenarios.

ActivatorModel: if your model requires fields like status, activate_date, and deactivate_date, you can use this base class. It also comes with a manager with .active () and .inactive () query sets enabled.

TitleDescriptionModel and TitleSlugDescriptionModel: these two models include the title and description fields, and the description field also includes slug, which is automatically generated based on the title field.

Django-extensions has more features that may be helpful to your project, so take a look at its documentation!

Configuration of 12-factor application: django-environ

In terms of Django project configuration, django-environ provides a management approach that conforms to the 12-factor application methodology. It is a collection of other libraries, including envparse and honcho. After installing django-environ, create a .env file in the root directory of the project and use it to define variables that vary from environment to environment, or variables that need to be kept secret. (such as API key, whether debugging is enabled, URL of database, etc.)

Then, introduce environ into the settings.py of the project, and set up environ.PATH () and environ.Env () with reference to the examples in the official documentation. The variable values defined in the .env file can be obtained through env ('VARIABLE_NAME').

Create an excellent administrative command: django-click

Django-click is based on Click, (we recommended it before. Click twice), which is very helpful for writing Django management commands. There is not much documentation in this library, but there is a directory in the code repository where test commands are stored, which is very valuable for reference. The basic Hello World command for django-click is written as follows:

# app_name.management.commands.hello.pyimport djclick as click @ click.command () @ click.argument ('name') def command (name): click.secho (f'Hello, {name}')

Call it on the command line so that it can be executed:

> >. / manage.py hello LaceyHello, Lacey handles finite state machine: django-fsm

Django-fsm adds finite state machine support to Django's model. If you run a news site and want to flow the status of articles like "writing", "editing" or "published", django-fsm can help you define these states and manage the rules and restrictions on state changes.

Django-fsm provides a FSMField field for the model to define the state of the model instance. With the @ transition modifier of django-fsm, you can define the method of state change and handle any side effects of state change.

Although django-fsm documents are lightweight, workflows (states) in Django this GitHub Gist article gives a very good introduction to finite state machines and django-fsm.

Contact form: # django-contact-form

The contact form can be said to be standard on the website. But don't write all the boilerplate code yourself, you can do it in a few minutes with django-contact-form. It comes with an optional form class that can filter spam (there is also a normal form class that does not filter) and a ContactFormView base class whose methods can be overridden or customized. It also guides you through the creation of the template to make the form work properly.

User registration and authentication: django-allauth

Django-allauth is a Django application that provides views, forms and URL for user registration, login / logout, password reset, and third-party user authentication (such as GitHub or Twitter), supports authentication of email address as user name, and has a large number of documentation. The configuration may be a little confusing when you use it for the second time; please read the installation instructions carefully, focus on customizing your configuration, and make sure that all the configurations that enable a feature are used correctly.

Dealing with user authentication of the Django REST framework: django-rest-auth

If Django development involves providing API, you probably use Django REST Framework (DRF). If you are using DRF, you should try django-rest-auth, which provides endpoints for user registration, login / logout, password reset and social media authentication (by adding django-allauth support, the two packages work well together).

API Visualization of Django REST Framework: django-rest-swagger

Django REST Swagger provides a feature-rich user interface for interacting with API of the Django REST framework. You just need to install Django REST Swagger, add it to the installed application of the Django project, then add Swagger view and URL mode to urls.py, and leave the rest to API's docstring.

API's user interface shows all endpoints and available methods in the dimensions of app, lists the available actions for these endpoints, and provides functionality to interact with API (such as adding / deleting / fetching records). Django-rest-swagger generates documents for each endpoint from docstrings in the API view, creating an API document for your project in this way, which is useful for you, front-end developers, and users.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report