Access Apache SetEnv variable from Django wsgi.py file

前端 未结 5 1781
盖世英雄少女心
盖世英雄少女心 2020-12-14 09:53

I\'m trying to separate out Django\'s secret key and DB pass into environmental variables, as widely suggested, so I can use identical code bases between local/production se

5条回答
  •  无人及你
    2020-12-14 10:40

    If anyone else is frustrated by Graham's answer, here is a solution that actually works for the original question. I personally find setting environmental variables from Apache to be extremely useful and practical, especially since I configure my own hosting environment and can do whatever I want.

    wsgi.py (tested in Django 1.5.4)

    from django.core.handlers.wsgi import WSGIHandler
    
    class WSGIEnvironment(WSGIHandler):
    
        def __call__(self, environ, start_response):
    
            os.environ['SETTINGS_CONFIG'] = environ['SETTINGS_CONFIG']
            return super(WSGIEnvironment, self).__call__(environ, start_response)
    
    application = WSGIEnvironment()
    

    Of minor note, you lose the future-proof method of django.core.wsgi.get_wsgi_application, which currently only returns WSGIHandler(). If the WSGIHandler.__call__ method is ever updated and you update Django also, you may have to update the WSGIEnvironment class if the arguments change. I consider this a very small penalty to pay for the convenience.

提交回复
热议问题