Home Web Front-end HTML Tutorial Summarize the experience of using python Django in development

Summarize the experience of using python Django in development

Feb 26, 2018 am 09:17 AM
django python

This time I will bring you a summarypython DjangoThe experience of using Django in development, what are the notes in python Django development, as follows This is a practical case, let’s take a look at it.

I first came into contact with Django when I was a junior in college. It has been almost 4 years since I actually used Django to do projects. My favorite is actually Django’s ORM framework. The company's projects are separated from front to back, and it is very efficient to use Django for back-end interface development.


Hereby summarize some experiences in Django development. Let’s talk about some of the most basics first.

Use virtualenv to isolate the development environment

Using pip to manage project dependencies is mainly a little trick. Use pip freeze > requirements.txt to save dependent modules and Version

Use the .gitignore file provided by gitignore.io to manage the code base file

Packaging and publishing

Packaging and publishing of the projectDocker, The Dockerfile of the Django project is very simple:

FROM python:3.5
COPY ./requirements.txt /src
WORKDIR /src
RUN pip install -r requirements.txt
COPY . /src
EXPOSE
CMD uwsgi --http :--wsgi-file<path/to/wsgi.py>
Copy after login

This Dockerfile template can cover 80% of the Django projects.

Log configuration

Since I use Docker, I have given up writing the log to the file and written it directly to the standard output.

   
# settings.py 
# ...
LOGGING = {
    &#39;version&#39;: 1,
    &#39;disable_existing_loggers&#39;: False,
    &#39;formatters&#39;: {
        &#39;verbose&#39;: {
            &#39;format&#39;: &#39;[application] %(levelname)s %(asctime)s %(module)s %(message)s&#39;
        }
    },
    &#39;handlers&#39;: {
        &#39;console&#39;: {
            &#39;level&#39;: &#39;DEBUG&#39;,
            &#39;class&#39;: &#39;logging.StreamHandler&#39;,
            &#39;stream&#39;: sys.stdout,
            &#39;formatter&#39;: &#39;verbose&#39;
        },
    },
    &#39;loggers&#39;: {
        &#39;app&#39;: {
            &#39;handlers&#39;: [&#39;console&#39;],
            &#39;level&#39;: &#39;DEBUG&#39;,
            &#39;propagate&#39;: True,
        },
    },
}
Copy after login


The new version of uwsgi can already collect webapp logs and output them to standard output. If you need to collect and manage logs, you can use the Docker log collection tool to directly collect the logs of the Docker container.

Automated testing

Since it is a pure back-end project, engineers can test their own code through automated testing. Django itself provides good support for testing. You can use sqlite to build a test database and memory-based cache. Testing will not increase dependence on other systems. Develop with less effort.

In addition to writing automated test code, you must also be able to count test coverage. Currently, we are using the tool coverage.py. To be honest, it is not as easy to use as istanbul of node.js, and the output report is not as detailed and easy to read as Istanbul. However, it is still useful for checking "dead code".

Testing for http code

Some projects need to connect to more third-party systems, such as WeChat authentication, payment, SMS and other common systems, and there may be systems in other vertical business fields. This part of the interface docking code should also be included in the test. After all, Python is a scripting language and the code is prone to errors.

This section generally uses the responses module to mock http requests.

Timing tasks

There are some Django projects that need to do some timing tasks. First of all, never use Linux’s built-in crontab. The main problem is that the maintenance cost is high, and this configuration may be forgotten one day.

Our current method is to use the function of Django Command to encapsulate scheduled tasks into a command. Run a scheduler in this command. Just like the following:

import schedule
from django.core.management.base import BaseCommand
class Command(BaseCommand):
    def handle(self, *args, **kwargs):
        schedule.every(45).minutes.do(do_this)
        schedule.every().day.at(&#39;04:00&#39;).do(do_that)
        while True:
            schedule.run_pending()
            time.sleep(1)
Copy after login

If you have any questions about this, you can ask me at any time. I have studied deeply about learning methods, systematic learning planning, and learning efficiency. I hope you can Help everyone avoid detours. The top three in the Python newbie exchange group are: 463, the middle three are: 024, and the last three are: 091

I believe you have mastered the method after reading these cases. Please pay attention for more exciting things. Other related articles on php Chinese website!

Related reading:

vue css animation

##Solution to the gap between the image and view tags

Why slots are used in subcomponents

How to use getBoundingClientRect() to achieve scrolling and fixation of div containers

The above is the detailed content of Summarize the experience of using python Django in development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles