Home > Backend Development > Python Tutorial > Global reference diagram of variables in settings.py in django

Global reference diagram of variables in settings.py in django

高洛峰
Release: 2017-03-28 16:08:18
Original
1825 people have browsed it

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
Copy after login

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爱好者,希望和大家一起学习,共同进步"
Copy after login

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}
Copy after login

Note:

The parameter request needs to be added to the function, otherwise the following error will occur:

Global reference diagram of variables in settings.py in django

3. Add the global_settings function to the OPTIONS configuration item in TEMPLATES in setting.py

Global reference diagram of variables in settings.py in django

4. Modify the template and directly access the corresponding variables through key names

Global reference diagram of variables in settings.py in django

5. Final effect:

Global reference diagram of variables in settings.py in django

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template