简体   繁体   中英

How to use the global variables in django?

I want to use word (english) dictionary in my Django application. However Django does not recommend using Global variables because of its threading model. This dictionary does not have thread-safety issues, I want to load the dictionary at the beginning and after it is constant (will be reading that from different Django views).

Is there any way to achieve this ?

You can put the dictionary in your static directory and put the path in your settings.py file. Then when you try to use it, you load the dictionary in your views.py .

Your can chose for it constants.py file created by yourself. For example,

# -*- coding: utf-8 -*-
SUMMER_MONTHS = {'june': 'June', 'july': 'July', 'august': 'August'}

And use it like this

import constants as CONSTS
print CONSTS.SUMMER_MONTHS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM