Global Reference of variables in settings. py in django, djangosettings. py

Source: Internet
Author: User

Global Reference of variables in settings. py in django, djangosettings. py

Add a custom variable in settings. py.Setting. (point) variable nameFor example:

From django. conf import settingssite_name = settings. SITE_NAMEsite_desc = settings. SITE_DESC

HoweverFrequent accessSuch:Email, website title, and website descriptionIn this way, access is inconvenient. solution:

1. First add the corresponding variable in settings. py:

# Website information SITE_NAME = "hupeng's blog" SITE_DESC = "pyhon fans, hope to learn together and make progress together"

2. Define the function in view and return the variables that contain 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::

You must add the request parameter to the function. Otherwise, the following error occurs:

 

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 using the key name.

5. Final Results:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.