Detailed explanation of the global reference method of variables in settings.py developed by Django

高洛峰
Release: 2017-03-31 09:36:05
Original
4463 people have browsed it

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

Note: Required in the function Add the parameter request, otherwise the following error will occur:

Detailed explanation of the global reference method of variables in settings.py developed by Django

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

Detailed explanation of the global reference method of variables in settings.py developed by Django

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

Detailed explanation of the global reference method of variables in settings.py developed by Django

5. Final effect:

Detailed explanation of the global reference method of variables in settings.py developed by Django

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!

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