0

I'm a total newbie and I'm trying to do this project this is my first time, and it's almost done. I tried every method mentioned in this SO thread to move secret key from settings. In every method i got some kind of error, even from this official django doc mathod. I couldn't find where I'm making mistake.

When the secret key is inside the settings.py, everything is working super smooth. But I need to push my code in git, so i have to hide it from settings.py.

Right now im adding the details when i tried using django-environ, to keep secret key outside of settings.py.

im putting the contents inside the root project folder. screenshot of root directory

im using miniconda: 4.10.1. here is my requirement.txt.

                # platform: linux-64
            _libgcc_mutex=0.1=main
            _openmp_mutex=4.5=1_gnu
            appdirs=1.4.4=py_0
            asgiref=3.3.4=pyhd3eb1b0_0
            attrs=21.2.0=pyhd3eb1b0_0
            black=19.10b0=py_0
            ca-certificates=2021.5.30=ha878542_0
            certifi=2021.5.30=py39hf3d152e_0
            click=8.0.1=pyhd3eb1b0_0
            django=3.2.4=pyhd3eb1b0_0
            django-environ=0.4.5=py_1
            importlib-metadata=3.10.0=py39h06a4308_0
            krb5=1.17.1=h173b8e3_0
            ld_impl_linux-64=2.35.1=h7274673_9
            libedit=3.1.20210216=h27cfd23_1
            libffi=3.3=he6710b0_2
            libgcc-ng=9.3.0=h5101ec6_17
            libgomp=9.3.0=h5101ec6_17
            libpq=12.2=h20c2e04_0
            libstdcxx-ng=9.3.0=hd4cf53a_17
            mypy_extensions=0.4.1=py39h06a4308_0
            ncurses=6.2=he6710b0_1
            openssl=1.1.1k=h7f98852_0
            pathspec=0.7.0=py_0
            pip=21.1.2=py39h06a4308_0
            psycopg2=2.8.6=py39h3c74f83_1
            python=3.9.5=h12debd9_4
            python_abi=3.9=1_cp39
            pytz=2021.1=pyhd3eb1b0_0
            readline=8.1=h27cfd23_0
            regex=2021.4.4=py39h27cfd23_0
            setuptools=52.0.0=py39h06a4308_0
            six=1.16.0=pyh6c4a22f_0
            sqlite=3.35.4=hdfb4753_0
            sqlparse=0.4.1=py_0
            tk=8.6.10=hbc83047_0
            toml=0.10.2=pyhd3eb1b0_0
            typed-ast=1.4.2=py39h27cfd23_1
            typing_extensions=3.7.4.3=pyha847dfd_0
            tzdata=2020f=h52ac0ba_0
            wheel=0.36.2=pyhd3eb1b0_0
            xz=5.2.5=h7b6447c_0
            zipp=3.4.1=pyhd3eb1b0_0
            zlib=1.2.11=h7b6447c_3

settings.py

                import os
            import environ
            from pathlib import Path

            env = environ.Env(
                # set casting, default value
                DEBUG=(bool, False)
            )

            # reading .env file
            environ.Env.read_env()

            # Build paths inside the project like this: BASE_DIR / 'subdir'.
            BASE_DIR = Path(__file__).resolve().parent.parent


            # Quick-start development settings - unsuitable for production
            # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

            # SECURITY WARNING: keep the secret key used in production secret!
            SECRET_KEY = env('SECRET_KEY')

            # False if not in os.environ
            DEBUG = env('DEBUG')

im not adding the rest of settings. i dont think its important. if need please mention I.ll update.

i placed .env file in root of the project where manage.py and db.sqlite3 are placed

.env

                #env file

            DEBUG=on

            #copied the entire line from settings.py
            SECRET_KEY ='xxxx django secret key here xxxx'

while running "python manage.py runserver", i got this error. here is the screenshot

im not sure what im missing. i got some kind of error, when i tried each method and errors are not same. sorry that i cannot explain every method and error here.

there are several questions asked in this form. but most are not answered and some are not accurately explains my situation. please mention if anything else is needed or for more clarification.

legacy
  • 191
  • 1
  • 9

2 Answers2

0

First check that you have installed django-environ and maybe you have a typing mistake in your requirements.txt it should be django-environ=0.4.5 instead of django-environ=0.4.5=py_1 you can pass the path of your .env inside read_env(env_file="relative_path_of_your_env_file")

it read a .env file into os.environ.

If not given a path to a dotenv path, does filthy magic stack backtracking to find manage.py and then find the dotenv.

go through this code https://github.com/joke2k/django-environ/blob/master/environ/environ.py#L614

Ankit Tiwari
  • 4,438
  • 4
  • 14
  • 41
  • requirement.txt is autogenarated by "conda list -e > requirements.txt" command. and while importing it in settings.py there wasnt an error. – legacy Jun 23 '21 at 16:58
  • i installed django-environ using this : "conda install -c conda-forge django-environ" – legacy Jun 23 '21 at 16:59
  • Okay let me check I have not use conda – Ankit Tiwari Jun 23 '21 at 17:00
  • Hello @legacy check this https://docs.conda.io/projects/conda/en/4.6.0/user-guide/tasks/manage-environments.html#windows to setup environment variables – Ankit Tiwari Jun 23 '21 at 17:07
  • in the first line of error there is a user warning. and they are looking for .env file inside the app directory. not in root. – legacy Jun 23 '21 at 17:41
  • check this to define path of your `.env` while running server https://django-environ.readthedocs.io/en/latest/#multiple-env-files – Ankit Tiwari Jun 23 '21 at 17:47
0

From the structure of the file tree, its clear that .env file is placed in the root folder of the project. When checking the error message, its visible that whoever is searching for .env file is checking at the same place as settings.py.

So, the short answer is if you are using django-environ to keep secret-key outside, place .env file together with settings.py in the same directory.

For a bit more elaborated content, you can refer to this link. I felt it is suitable for newbies.

legacy
  • 191
  • 1
  • 9