Summarize the experience of using python Django in development
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>
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 = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': '[application] %(levelname)s %(asctime)s %(module)s %(message)s' } }, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'stream': sys.stdout, 'formatter': 'verbose' }, }, 'loggers': { 'app': { 'handlers': ['console'], 'level': 'DEBUG', 'propagate': True, }, }, }
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('04:00').do(do_that) while True: schedule.run_pending() time.sleep(1)
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:
##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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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 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 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.

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.

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.

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.

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".
