Skip to content

Use environment variable to set application context root

Chris edited this page Jan 3, 2018 · 12 revisions

Context

When installing the reverse proxy to point to our servers, it was requested that the url should include a reference to gwells. So the prod url would look like https://<prodroot>/gwells

Django does not automatically agree with such a treatment and must be adapted. It is fairly easy for do that in the code and configuration but it was deemed wise to push the gwells string into an environment variable in OpenShift which Django then can read and apply.

Steps to implement

  1. Create/Set Environment Variable in OpenShift
  2. Update Django
  3. Set local activate.bat (virtualenv setting)
  4. Test

1. Create/Set Environment Variable in OpenShift

  • Select the environmnet you want to set the variable for. (e.g. Groundwater Wells (Dev)).
  • Select "Overview" from the site menu
  • Open drop down with the systems's URL (e.g. https://gwells-dev.pathfinder.gov.bc.ca/gwells)
  • Select url in "Deployment Config gwells"
  • In the new window, choose "Environment", you have now arrived on the Environment variables page
  • Create new variable "APP_CONTEXT_ROOT"
  • Set the value to "gwells"

2. Update Django

  • In settings.py add:

# Controls app context APP_CONTEXT_ROOT = os.getenv('APP_CONTEXT_ROOT','')

`* and change the setting for the statisc files:`

if APP_CONTEXT_ROOT: STATIC_URL = '/'+ APP_CONTEXT_ROOT +'/static/' else: STATIC_URL = '/static/'

  • in url.py change the urls to include the APP_CONTEXT_ROOT setting

# Creating 2 versions of the app_root. One without and one with trailing slash # This will allow for any or no additional app_root context to be provided app_root = settings.APP_CONTEXT_ROOT if app_root: app_root_slash = app_root + '/' else: app_root_slash = app_root

urlpatterns = [ url(r'^'+ app_root +'$', views.HomeView.as_view(), name='home'), url(r'^'+ app_root_slash +'$', views.well_search, name='home'), ....

3. Set local activate.bat (virtualenv setting)

  • Add:

SET APP_CONTEXT_ROOT=gwells

4. Test

  • Start your local Django, navigate
  • Set and reset the APP_CONTEXT_ROOT to test the difference
Clone this wiki locally