Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define Constants in settings.py and access them in views function in django

Tags:

python

django

I want to know how can i define some constants in django and use them in view.py function. I have some variables which are never going to be changed and are used in a function many a time. So i decided to define them as constants.

If any one can help me with the declaration of constants and how to import and use them in a function in views.py

EDIT:

constant.py

total_range='A1:C15'

view.py

from constant import *
def myFunction():
    ***Code to access google spreadsheet**
    cell_value_list=worksheet.range(total_range)

here it gives me an error saying total_range is not defined.

like image 359
Aman Gupta Avatar asked Dec 04 '22 02:12

Aman Gupta


1 Answers

Define a variable like this i settings.py:

MY_VAR = "MY_VALUE"

Import settings in views.py:

from django.conf import settings

You can use your variable in views.py like this:

settings.MY_VAR

You can make a seprate file beside views.py and define all constants on it.

for example create constants.py like this:

const1 = 'value1'
const2 = 'value2'
.
.
.

Now you can use constants in views.py like this:

from constants import *

print const1
like image 87
Hasan Ramezani Avatar answered Jan 28 '23 14:01

Hasan Ramezani