Global Reference of variables in settings. py in django, djangosettings. py
Add a custom variable in settings. py.Setting. (point) variable nameFor example:
From django. conf import settingssite_name = settings. SITE_NAMEsite_desc = settings. SITE_DESC
HoweverFrequent accessSuch:Email, website title, and website descriptionIn this way, access is inconvenient. solution:
1. First add the corresponding variable in settings. py:
# Website information SITE_NAME = "hupeng's blog" SITE_DESC = "pyhon fans, hope to learn together and make progress together"
2. Define the function in view and return the variables that contain the settings configuration file.
From django. conf import settings
Def global_settings (request): return {"SITE_NAME": settings. SITE_NAME, "SITE_DESC": settings. SITE_DESC}
Note::
You must add the request parameter to the function. Otherwise, the following error occurs:
3. Add the global_settings function to the OPTIONS configuration item in TEMPLATES in setting. py.
4. Modify the template and directly access the corresponding variables using the key name.
5. Final Results: