To add a custom variable in settings.py, you can pass setting. (dot) variable name access, such as:
From django.conf Import settingssite_name = settings. Site_namesite_desc = settings. Site_desc
However, if you encounter a number of frequently accessed variables, such as: Mailbox, site title, the description of the site, so access is very inconvenient, workaround:
1. First add the corresponding variable in the settings.py:
#网站信息SITE_NAME = "Hupeng's personal blog" site_desc= "Pyhon enthusiasts, hope to learn together with you and make progress together"
2. Define the function in view and return the variable containing the settings configuration file
From django.conf import settingsdef global_settings (Request): return {"Site_Name": Settings. Site_Name, "Site_desc": Settings. SITE_DESC}
Attention:
The parameter request needs to be added to the function, otherwise the following error will occur:
3. Add the Global_settings function to the options configuration item in templates in setting.py
4. Modify the template to directly access the corresponding variable by key name
5. Final effect: