Home >Backend Development >Python Tutorial >Detailed explanation of the global reference method of variables in settings.py developed by Django
When some content in the website, such as email, website title, website description, we can store these things in the database or in our setting file. This article mainly introduces to you django# The relevant information about the global reference of variables in settings.py is introduced in great detail in the article. Friends who need it can refer to it.
This article mainly introduces the relevant information about the global reference of variables in settings.py in django. I won’t say much below, let’s take a look at the detailed introduction.Preface
Add self-define variables in settings.py, you can pass setting.(dot)Variable name access, such as:
from django.conf import settings site_name = settings.SITE_NAME site_desc = settings.SITE_DESCHowever, if you encounter some frequently accessed variables, such as: email, website title, website description, such access will be very inconvenient.
The solution is as follows:
1. First, add the corresponding variables in settings.py:#网站信息 SITE_NAME="hupeng的个人博客" SITE_DESC="pyhon爱好者,希望和大家一起学习,共同进步"2.
Define the function in the view and return the variables in the settingsconfig file
from django.conf import settings def global_settings(request): return {"SITE_NAME": settings.SITE_NAME, "SITE_DESC": settings.SITE_DESC}
Note: Required in the function Add the parameter request, 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 Detailed explanation of the global reference method of variables in settings.py developed by Django. For more information, please follow other related articles on the PHP Chinese website!