Home > Article > Backend Development > Global reference diagram of variables in settings.py in django
Add custom variables in settings.py, which can be accessed through setting. (dot) variable name, such as:
from django.conf import settings site_name = settings.SITE_NAME site_desc = settings.SITE_DESC
However, if you encounter some frequently accessed variables, such as: email, Website title, website description, it is very inconvenient to access this way. Solution:
1. First add the corresponding variables in settings.py:
#网站信息SITE_NAME="hupeng的个人博客"SITE_DESC="pyhon爱好者,希望和大家一起学习,共同进步"
2. Define the function in the view , returns the variables in 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:
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 and directly access the corresponding variables through key names
5. Final effect:
The above is the detailed content of Global reference diagram of variables in settings.py in django. For more information, please follow other related articles on the PHP Chinese website!