SoFunction
Updated on 2025-03-03

How to trigger static pages through timed tasks in Django

There are many different web frameworks under Python. Django is the most representative of the heavyweights. Many successful websites and apps are based on Django.
Django is an open source web application framework written in Python.
Django complies with BSD copyrights and was first released in July 2005, and released its first official version 1.0 in September 2008.
Django adopts the software design pattern of MVC, namely model M, view V and controller C.

This chapter introduces you to the processing method of triggering static pages through timed tasks in Django. The specific content is as follows:

Install

pip install django-crontab

Add an app

INSTALLED_APPS = [
  ...
  'django_crontab', # Timing tasks  ...
]

Set the timing time of the task

Set the time to execute in the configuration file

Each timing task is defined in three parts:

Mission time

Basic format:

* * * * *

Time Day Month Week Order

M: Minutes (0-59). Expressed by * or */1 per minute

H: hours (0-23). (0 means 0 points)

D: Day (1-31).

m: Month (1-12).

d: Days within one week (0~6, 0 is Sunday).

Task method

Task log

If the timing task is implemented on the home page, the following

# Timing tasksCRONJOBS = [
  # Generate home page static files every 5 minutes  ('*/5 * * * *', 'Execute the function that generates static page', '>> Path to generate log')
]

untieDetermine Chinese characters

In a timing task, if non-English characters appear, a character exception error will occur

It can be implemented by adding additional commands for timing tasks to execute in the configuration file.

# Solve crontab Chinese problemCRONTAB_COMMAND_PREFIX = 'LANG_ALL=zh_cn.UTF-8'

Turn on timing tasks

Add timing tasks to the system

python  crontab add

Displays activated timing tasks

python  crontab show

Remove timing tasks

python  crontab remove

After the timing task is enabled, a log log will appear in the log log every five minutes.

Summarize

The above is the static processing method of triggering pages in Django introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!