Tuesday, June 26, 2012

PROBLEM: Variable of settings.py imported from the Environment does not change, although it has different values in two different VirtualHosts

Hello everybody,

I would like to ask you how to solve an issue that is actually driving me crazy. I have found several threads and workarounds of people who seem to have faced a similar problem, but I havent actually found a valid solution to my problem. Here it is:

1. I have several VirtualHosts defined in my Apache configuration file. Each of these VirtualHosts try to access a GUI which has been designed using Python and mod_wsgi. In these Apache configuration files, I have defined an Environment Variable which I then use in the wsgi script


SetEnv MY_VAR my_var_name

...

WSGIScriptAlias /gui "/guipath/django.wsgi"



2. Googling a bit, I found a guy answering how to actually use Environment Variables in the templates. In this sense, it was necessary to set them in the WSGI script file. Below is my WSGI file:


import os
import sys

path = os.path.dirname(os.path.abspath(__file__)) + '/..'
path2 = os.path.dirname(os.path.abspath(__file__))
if path not in sys.path:
    sys.path.append(path)
if path2 not in sys.path:
    sys.path.append(path2)

os.environ['DJANGO_SETTINGS_MODULE'] = 'gui.settings'

import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()

def application(environ, start_response):
  os.environ['VAR_NAME'] = environ['MY_VAR']
  return _application(environ, start_response)


3. Then, in settings.py, I am actually loading this variable in order to be used from the views.py file


VARIABLE = os.environ['VAR_NAME']


4. Finally, I import the settings file in the views file, and I then load the value of VARIABLE in each of the views.


All the process seems to work OK, except for the following problem. 

- In my case, each VirtualHost has been configured to have a different IP, and a different MY_VAR value, and all these IPs can actually access to the GUI. The problem arises when I try to switch from one IP to another. Let us assume the case of two different virtual hosts. Let us call them VH1 and VH2.
- If I type the IP corresponding to the GUI of the first virtual host, I receive for the first time


"WELCOME TO THE VH1 GUI"


- Then, I switch to the IP corresponding to the second virtual host. The result I receive is then


"WELCOME TO THE VH1 GUI" 


- Now, if I restart Apache, and I reload this second IP, y receive:


"WELCOME TO THE VH2 GUI"  

- I have checked whether the environment variable assignation was working or not, by printing the value in the WSGI script, and the value was changing each time I switched from one virtual host to the other. So the problems seems to be that settings.py is loaded just once at the beginning, and the variables are not refreshed any more.

Could you please tell me a way to solve this problem?? I would be extremely grateful!!!!

Thank you very very much in advance.

With kind regards,
Alvaro

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/OCw0ix7LUzsJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate