如何将环境变量添加到Django项目 [英] How do you add environment variables to your django project

查看:275
本文介绍了如何将环境变量添加到Django项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置项目,以便它可以在本地使用环境变量

I'm trying to set up my project so that it can use environment variables locally

我尝试将其添加到我的激活文件的末尾并列出其他事情。我正在尝试使用此

Ive tried adding it to the end of my activate file and a list of other things. I'm trying to use this

from .base import *

    if os.environ['DJANGO_SERVER_TYPE'] == 'local':
        try:
            from .local import *
        except:
            pass

    if os.environ['DJANGO_SERVER_TYPE'] == 'production':
        try:
            from .production import *
        except:
            pass

我是一个真正的新手,常常会简要地解释事实。因此,全面的解释对我如何实现此感谢确实非常有益。 Bash我从来没有做过任何事。我已经尝试过这样做

I am a real novice and often things are explained brief and matter of factly. So a thorough explanation would really be beneficial to me in how to implement this thanks. I've NEVER done anything n Bash. Ive tried doing this

export KEY=VALUE

在激活文件中,只是因为它不被系统识别,我必须将其删除才能使用本地服务器

in the activate file, only for it not to be recognized by the system, and I had to remove it in order to use my local server

推荐答案

如果通过Django Web服务器运行它,则可以将环境变量传递给其他命令的方式与之相同:

If you're running it through the Django web server, you can pass environment variables the same way you would to any other command:

DJANGO_SERVER_TYPE="local" ./manage.py runserver

如果通过像Apache这样的Web服务器运行它,则可以通过虚拟主机配置来设置环境变量:

If you're running it through a web server like Apache, you can set environment variables through your virtual host configuration:

SetEnv DJANGO_SERVER_TYPE local

这篇关于如何将环境变量添加到Django项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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