Page Static Processing Mode Triggered by Timing Task in Django
- 2021-11-01 02:34:05
- OfStack
There are many different Web frameworks under Python. Django is the most representative of heavyweights. Many successful websites and APP are based on Django.
Django is an open source Web application framework written by Python.
Django, copyrighted by BSD, was first released in July 2005 and the first official version 1.0 was released in September 2008.
Django adopts the software design pattern of MVC, namely model M, view V and controller C.
This chapter introduces the static processing mode of triggering pages through timed tasks in Django. The specific contents are as follows:
Installation
pip install django-crontab
Add an application
INSTALLED_APPS = [
...
'django_crontab', # Timing task
...
]
Set the timing time of the task
Set the timed execution time in the configuration file
Each timed task is defined in three parts:
Task time
Basic format:
* * * * *
Time-sharing sun, moon and week order
M: Minutes (0-59). Each minute is denoted by * or */1
H: Hours (0-23). (0 denotes 0 point)
D: days (1-31).
m: Months (1-12).
d: Days in a week (0 ~ 6, 0 is Sunday).
Task method
Task log
For example, the timing task setting of the homepage of the webpage is as follows
# Timing task
CRONJOBS = [
# Every 5 Execute in minutes 1 Second generation of home page static file
('*/5 * * * *', ' Execute functions that generate static pages ', '>> The path to generate the log ')
]
Solve the problem of Chinese characters
In a timed task, if a non-English character appears, a character exception error will occur
You can do this by adding additional commands to the configuration file for timed task execution
# Solve crontab Chinese problem
CRONTAB_COMMAND_PREFIX = 'LANG_ALL=zh_cn.UTF-8'
Start Timing Task
Adding Timing Tasks to the System
python manage.py crontab add
Displays active timed tasks
python manage.py crontab show
Remove Timed Task
python manage.py crontab remove
When the timer task is turned on, an log log appears in the log log every 5 minutes
Summarize