The global reference method of variables in the settings.py of Django development

Source: Internet
Author: User
When some content inside the website, such as the mailbox, the website title, the description of the website, these things we can exist in the database can also be stored in our setting file, This article mainly introduces you to the settings.py variables in the Django Global reference data, the article introduced in very detailed, the need for friends can refer to.

This article is mainly about Django in the settings.py of variables in the global reference to the relevant data, the following words do not say, to see the detailed introduction it.

Objective

To add a custom variable in settings.py, you can pass setting. (dot) variable name access, such as:

From django.conf Import settingssite_name = settings. Site_namesite_desc = settings. Site_desc

However, if you encounter a number of frequently accessed variables, such as: Mailbox, site title, the description of the site, so access is very inconvenient.

Here's how to fix it:

1, first add the corresponding variables in the settings.py:

#网站信息SITE_NAME = "Hupeng's personal blog" site_desc= "Pyhon enthusiasts, hope to learn together with you and make progress together"

2. Define the function in view and return the variable containing the settings configuration file

From django.conf import settingsdef 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, through the way the key name directly access the corresponding variable

5. Final effect:

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.