PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 19, 2022

[FIXED] Where do I set environment variables for Django?

 August 19, 2022     bash, django, environment-variables, ubuntu     No comments   

Issue

everyone!

Django 1.11 + PostgreSQL 9.6 + Gunicorn + Ubuntu 16.04 in AWS

I want to set environment variables for sensitive info.(django secret key, DB password...)

I studied many articles about setting ways.

But when I tried os.environ['env_name'],

  1. .bashrc: Not working

  2. .bash_profile: Not working

  3. .profile: Not working

  4. /etc/environment: Not working

  5. Gunicorn script file.(systemd): I set them in gunicorn systemd script. It work very well.

But because I want to use the environment variables in other program too, I set them among 1~5 configurations. I don't understand why 1~5 configurations didn't work. Is there scope or priority of setting environment variables?

EDIT:

I use Ubuntu 16.04 server. I can't restart terminal session.

I tried 'source .bashrc' and logout/login. But It didn't work.

Of cource, 'echo $some_env_var' is working, I say, django can't read.


Solution

create a file called .bashrc in your server

export('the_name_in_bashrc', some_value)

then in the settings.py

import os
some_variable = os.environ.get('the_name_in_bashrc')


Answered By - Exprator
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing