简体   繁体   English

为django项目设置环境变量

[英]Setting environment variables for a django project

I am using PyCharm on Windows and want to set the environment variables for Django website.我在Windows上使用PyCharm,想给Django网站设置环境变量。 I have tried two approaches:我尝试了两种方法:

1. activate.bat 1.激活.bat

I am setting the values in the project's venv:我在项目的 venv 中设置值:

在此处输入图像描述

activate.bat激活.bat

@echo off

set "VIRTUAL_ENV=D:\Git\QV-PublicWebsite\venv"
set DJANGO_SECRET_KEY='random'
...

activate.ps1激活.ps1

...
$VIRTUAL_ENV = $BASE_DIR
$env:VIRTUAL_ENV = $VIRTUAL_ENV
$env:DJANGO_SECRET_KEY='random'
...

When I load the project in PyCharm, I can see that the venv is active.当我加载 PyCharm 中的项目时,我可以看到 venv 处于活动状态。 在此处输入图像描述

But when I start debugging the project, I am not able to read the values:但是当我开始调试项目时,我无法读取这些值: 在此处输入图像描述

This is the settings that I have for debugging the project:这是我用于调试项目的设置: 在此处输入图像描述

2. Env File 2. 环境文件

I have also tried using EnvFile tab and import my .env file我也尝试过使用 EnvFile 选项卡并导入我的.env文件

在此处输入图像描述

This way, when I debug the project in PyCharm, the environment variables are set as expected.这样我调试PyCharm的项目时,环境变量就设置好了。

However when I want to build the migrations then the environment variables are not set again!但是,当我想构建迁移时,不会再次设置环境变量!

在此处输入图像描述

If you want to add a virtual environment for your Django Website the simple way is as follows:-如果你想为你的 Django 网站添加一个虚拟环境,简单的方法如下:-

->. ->。 Create Environment variable by: python -m venv <virtual_env_name>通过以下方式创建环境变量: python -m venv <virtual_env_name>

->. ->。 After the Virtual environment got created you have 2 approaches to activating it创建虚拟环境后,您有两种激活它的方法

i.By using CMD. cd <virtual_env_path>\Scripts\activate.bat [NOTE: Please make sure to use Backward slash '\'] i.通过使用 CMD.cd <virtual_env_path>\Scripts\activate.bat [注意:请确保使用反斜杠'\']

ii.二. By using Pycharm interpreter - go to settings in Pycharm than select interpreter settings and add python interpreter - in add settings select interpreter and in scripts dir choose python.exe and you are ready to use your virtual environment通过使用 Pycharm 解释器- go 到 Pycharm 中的设置而不是 select 解释器设置并添加 python 解释器 - 在添加设置 select 解释器和脚本目录中选择88434654你准备好使用虚拟和 exe 环境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM