I am doing the official django tutorial polls app on part 5 which is about testing in django. As a part of the instructions I am supposed to go into my models.py file and run the python manage.py shell command to activate a shell terminal. From there I am supposed to run a series of commands to import things for my test.
>>> import datetime
>>> from django.utils import timezone
>>> from polls.models import Question
>>> # create a Question instance with pub_date 30 days in the future
>>> future_question = Question(pub_date=timezone.now() + datetime.timedelta(days=30))
>>> # was it published recently?
I run the import datetime, and from django.utils import timezone and there are no issues.
Once I get to from polls.models import Question I am hit with an error message no modulenotfounderror" no module named dot env.
Yes that is correct I have an outer directory mysite and an inner directory with a folder named mysite. the polls app is in the mysite directory.
Urls.py is located in the project root folter or inner mysite folder and there is a urls.py file for the polls app as well.
I am pretty certain you said no screenshots were allowed for code, but what about folder directory assistance?
If not my github repository for this is
in my settings.py there are the following lines of code.
from dotenv import load_dotenv, find_dotenv
dotenv is underlined in yellow and is showing as not defined.
it is throwing an error for my program.
i set up a virtual enviornment
and ran the pip install python-dotenv and my dotenv is acting like it is not importina and my code is not working when I try to run the commands listed on part 5 of the tutorial.
when I try to run python manage.py shell and I get
After reviewing the python documentation I think I figured it out. Key Error means hey there is no key holding this information. I borrowed the line of code shown below because it was helping me with my secret_key setup. But the thing is I don’t have access to that dictionary data. Either I need to revisit the instructions or find another way to set up my secret_key.
Before checking the documentation that was a line of code someone gave me as part of a setup for a way to hide my secret key.
Per the documentation,
A mapping object where keys and values are strings that represent the process environment. For example, environ['HOME'] is the pathname of your home directory (on some platforms), and is equivalent to getenv("HOME") in C.
In my own words it seems like this is an object of some sort that influences the environment variables in my project.
per the documentation: This mapping may be used to modify the environment as well as query the environment. putenv() will be called automatically when the mapping is modified.
I think this reinforces what I suspect.
Thinking this through my question are,
what are enviornment variables? → I will be looking that up in the python documentation
“We can use Python os module “environ” property to get the dictionary of all the environment variables. When the os module is loaded by Python interpreter, the environ value is set. Any further changes in the environment variables through external programs will not get reflected in the already running Python program.”
It can - more commonly, it’s used to give your program access environment variables. (Changing the environment in Django doesn’t do you a lot of good. The value in Django is allowing it to read those environment variables.)
That’s a good place to start, but you should be aware that environment variables exist for every program that runs.
Please, do yourself a favor and do not rely up SO as a primary source of information. Save yourself some headaches and always double-check anything you see on there from a primary source.
Assuming you’re referring to:
It’s getting the environment variable named SECRET_KEY, and assigning it to the settings variable named SECRET_KEY.
Until you are comfortable with environment variables and how they’re used, you can save yourself some problems by just assiging a secret key in your settings.
so the solution may be to remove this code and use git ignore to stop my secret_keys from being uploaded to github
I need to check the documentation about the best way to save my secret keys. leaving it in settings.py leaves the secret key getting uploaded to github. I have been told to use a txt file and gitignore but if there is a more conventional way I am open to it.
Since we’re talking about a tutorial here, you could simply ignore the security issues with your SECRET_KEY being in github. Yes, it’s not something you want to do with a real system - but while you’re learing your way through things, this is one of those issues that can safely be ignored.