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?
>>> future_question.was_published_recently()
True
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.
To confirm, youâre in the âouterâ directory named âmysiteâ. In that directory you have directories named âmysiteâ and âpollsâ, is that correct?
You have a settings.py file in the inner âmysiteâ directory?
Additionally, you have a urls.py file in both of those two directories?
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
i ran the pip install python-dotenv command just now as well as updating the time zone.
I wonder why I am having these issues. I have pushed these changes up to my repo.
it seems that from dotenv is not working. however the load_dotenv, and find_dotenv is no longer having yellow lines underneath it. Is there some way for me to get the dotenv working?
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
KeyError: âSECRET_KEYâ
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.