Reputation: 110267
Here is the code I'm currently using to test whether my website is deployed locally, on staging, or production:
def get_base_url(request=None):
BASE_URL = request.META.get('HTTP_HOST') if request else settings.STATIC_SITE_URL
if not BASE_URL.startswith('http'):
BASE_URL = 'http://' + BASE_URL # such as "localhost:8000"
return BASE_URL
And to do something like send a link for a password reset, I would do:
BASE_URL = get_base_url(request)
PATH = BASE_URL.rstrip('/') + reverse('logged_in')
Is there a cleaner way to do this? I tried a few others ways but this seems to return the most correct and consistent result.
Upvotes: 0
Views: 59
Reputation: 4267
From my experience in deploying many Django apps to production, the best approach would be to separate settings into different modules. It is also very good described in Two Scoops of Django in Using Multiple Settings Files
chapter.
settings/
base.py
local.py
stage.py
production.py
local
, stage
, production
inherit from base
.
For example local.py
:
from .base import *
DEBUG = True
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db_name',
'HOST': 'localhost',
}
}
INSTALLED_APPS += ['debug_toolbar', ]
BASE_URL = 'http://localhost:8080/'
For example production.py
:
from .base import *
DEBUG = False
...
BASE_URL = 'https://example.com'
For example stage.py
:
from .base import *
DEBUG = False
BASE_URL = 'https://stage.example.com'
After this you can just set BASE_URL
for each specific environment in each settings file and access in from settings.BASE_URL
everywhere you want.
If will make your life much easier and allow to configure you settings depending on environment very dynamically.
Upvotes: 1