0

I am learning Django and I tried to create a template like this:

>>> weekend = True
>>> from django.template import Template, Context
>>> template_string = """
{% if weekend %}
    <p> This is a weekend </p>
{% endif %}
"""
>>> t = Template(template_string)

I get this following error. Am I missing something?

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    t = Template(template_string)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 106, in __init__
    if settings.TEMPLATE_DEBUG and origin is None:
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 276, in __getattr__
    self._setup()
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

Thanks!

2
  • 1
    "ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined." Seems pretty clear. Are you following the official Django tutorial? If not, what tutorial are you following? Commented Apr 21, 2011 at 11:16
  • Yes, I am using the book from djangobook.com. Sorry, my bad. Missed it out. Just started IDLE instead Commented Apr 21, 2011 at 11:19

2 Answers 2

2

This has nothing to do with templates.

Start your shell by doing python manage.py shell from the directory containing manage.py and settings.py.

Sign up to request clarification or add additional context in comments.

Comments

0

If you've started a django project try running the same code inside a django shell.

python manage.py shell

Alternatively set DJANGO_SETTINGS_MODULE to the name of your settings module. If you're using linux and running things from the directory where the settings.py file is then you can just do

export DJANGO_SETTINGS_MODULE="settings"

If you're using linux (or another unix based OS) and you want to run from a different directory you'll need to make sure your project directory is in the python path. Then you can do

export DJANGO_SETTINGS_MODULE="myapp.settings"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.