从Django的wsgi.py文件访问Apache的变量SETENV [英] Access Apache SetEnv variable from Django wsgi.py file

查看:103
本文介绍了从Django的wsgi.py文件访问Apache的变量SETENV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分离出Django的密钥和DB传递到环境变量,尽可能广泛的建议,这样我就可以使用本地/生产服务器之间是相同的code碱基。

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 servers.

我遇到了正确的设置,然后读取运行Apache + mod_wsgi的生产服务器上的环境瓦尔的问题。

The problem I am running into is correctly setting and then reading the environmental vars on the production server running Apache + mod_wsgi.

瓦尔在我的用户个人资料设置不可用,因为Apache不作为用户运行。瓦尔在虚拟主机文件中设置与 SETENV 所不具备的,因为范围有些不同。

Vars set in my user profile aren't available because Apache isn't run as that user. Vars set in the Virtual Hosts file with SetEnv are not available because the scope is somehow different.

我读过一对夫妇<一个href=\"http://stackoverflow.com/questions/16792698/os-environ-in-django-settings-py-cannot-get-system-environment-variables-with\">1,2如此回答,从而导致这个博客一个解决方案。

I've read a couple 1,2 of SO answers, which leads to this blog with a solution.

我无法弄清楚如何解决应用到的Django的当前版本它们使用 wsgi.py 文件,该文件是这样的:

I can't figure out how to apply the solution to current versions of Django which use a wsgi.py file, which looks like:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

如何申请了博客解决wsgi.py文件,或者是有一个更好的地方来存储ENV-瓦尔Django在何处可以在他们那里得到?

How can I apply that blog solution to the wsgi.py file, or is there a better place to store env-vars where Django can get at them?

推荐答案

如果别人是格雷厄姆的回答感到沮丧,这里要说的是实际工作对原来的问题的解决方案。我个人觉得来自Apache设置环境变量是非常有益和实用的,尤其是因为我配置我自己的托管环境,可以做我想做的事情。

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(Django的1.5.4测试)

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()

轻微值得注意的是,你失去 django.core.wsgi.get_wsgi_application 的面向未来的方法,目前只返回 WSGIHandler()。如果 WSGIHandler .__呼叫__ 方法是不断更新,并更新Django的同时,您可能需要更新 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.

这篇关于从Django的wsgi.py文件访问Apache的变量SETENV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆