If you've stumbled upon my article, I assume you are working on a Django project and are wondering how to secure your project information, more imp...
For further actions, you may consider blocking this person and/or reporting abuse
Good tip. My solution is to commit a
settings_base.py
file with the Django project. On each deployed system (including local) I then have asettings.py
file which imports the base file and overrides anything secret or unique to the system.Your solution is also very convenient when working with docker. It's common practice to pass settings into containers using environment variables.
That's also a very viable solution. Thank you for the comment!
If you want to set default value, you can use like
Then you don't need more settings for production and development.
Just on and off ```
load_dotenv()
This really helped me a lot!
That's pretty clever, I'll be sure to keep it in mind for my next project
I referenced it from mdn ;)
Have a nice day.
pretty cool either way. thank you, and likewise!
Hi there, I joined this forum just to ask this question :-) about this fantastic tip in this post:
My question relates to adding the .env file into .gitignore. How can an app run without the SECRET coming along in production? I read somewhere that a new .env file needs to be created on the production site. In that case, what's the difference between uploading the .env along with the rest of the app and creating a new .env file? Is the concern mostly over the security during uploading?
Thanks for your comment and welcome to Dev! So from my understanding, you already have a .env file in your development server. If that .env file works for you in development, keep that since it will most likely work for production as well. Any time I develop anything for production, I try to keep the .env consistent and hidden in the .gitignore. There shouldn't be any difference between uploading the development .env for production, as long as Django knows where to find your .env.
The biggest security concern that will come up in regards to the environment secrets is accidentally committing them to Github. Let me know if that clears up your question.
Happy Coding!
Yes, this totally cleared up the fog on my end: Exposing the .env on GitHub is a security risk (as opposed to having .env on a server). Thank you for taking the time to explain! And yes, I followed the steps in this post and everything works beautifully. Since I read your post and became aware of the issue of protecting Django secret, I've read other posts on how to do it-but none is as clean and easy to follow. So thank you for bringing this issue to the attention of new Django learners.
It is absolutely my pleasure! So glad it helped :)
I followed all of the instructions and copy and pasted everything but I get the error ModuleNotFoundError: No module named 'dotenv'. Any idea why this might be?
Hey! Thanks for your comment. One possibility might be you didn't install the module, which can be done with
pip3 install python-dotenv
. Another possibility, if you already installed the module, your requirements might not have been updated yet which can be done withpip3 freeze > requirements.txt
Let me know if you continue receiving the error, hopefully, this helped.
I appreciate the response! I installed the module but I think I figured out that I did not install Django correctly in the context of the application and that was causing the issue. I will update with more errors if needed.
This was a really helpful article, thank you! If you write anything else on Django security in the future, I'll definitely give it a read.
Thank you! I really appreciate the feedback π
Great tip. A question.
How do i put my .env file in the gitignore?
you would just list it plainly as that: .env (unless it's not in the same directory as the gitignore file of course)
Ok thanks for your response. I have 2 questions.
First, I can't seem to locate the 'gitignore file' on my django project directory.
Second is, the code:
from dotenv import load_dotenv
load_dotenv()
it brings an error saying "unresolved import 'dotenv' in my problem terminal.
I did install dotenov and correctly too. You might have an answer for me? I'm a beginner with django. Thank you
The .gitignore file is one you have to manually create, it doesn't come with the initial Django project directory. Make sure its in the same directory as your manage.py file, along with requirements, etc.
For the first one, the .gitignore file doesn't come with the initial Django project - you have to manually create it. Make sure it's in the same directory as the manage.py, requirements, etc.
For the dotenv, do the errors still show up after you run:
pip freeze > requirements.txt
?
Thank you, i fixed it already. Thanks.