When some content inside the website, such as the mailbox, the website title, the description of the website, these things we can exist in the database can also be stored in our setting file, This article mainly introduces you to the settings.py variables in the Django Global reference data, the article introduced in very detailed, the need for friends can refer to.
This article is mainly about Django in the settings.py of variables in the global reference to the relevant data, the following words do not say, to see the detailed introduction it.
Objective
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.
Here's how to fix it:
1, first add the corresponding variables 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}
Note: 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, through the way the key name directly access the corresponding variable
5. Final effect: