Reputation: 2586
def get(self , request , format=None):
body = request.data
name = body.get('name',"The_Flash")
In this instance I have hardcoded the value The_Flash if the request.data receives no value for name , but I know this is not a sound way. I want this to be added as a variable in the settings.py file of my django project. I went through references from SO like this and few others but this is not what I want. Can someone tell me which is the most robust way of doing this. I am using Django 1.8.
Upvotes: 2
Views: 3579
Reputation: 1649
In views.py file:
from django.conf import settings
settings.Variable_Name
Upvotes: 0
Reputation: 6777
We tend to store settings variables in a module in the app called app_settings.py
, and use this to set defaults for settings and allow the user to override them in Django's settings.py
:
# app_settings.py
from django.conf import settings
MY_SETTING = getattr(settings, 'APP_NAME_MY_SETTING', 'the_default_value')
Then import this in your views and use it:
# views.py
from app_settings import MY_SETTING
And users can override it in their project settings:
# project's settings.py
APP_NAME_MY_SETTING = 'something else'
This allows you to change it per deployment, etc.
Upvotes: 6
Reputation: 1482
You can store constants in a seperate file and import it into your project
folder/
appconfigurations.py
views.py
appconfigurations.py
YOUR_CONSTANT = "constant_value"
views.py
from appconfigurations import *
def your_view(request):
constant = YOUR_CONSTANT
Upvotes: 1