0
from django import template

t = template.Template("My name is {{ name }}.")
c = template.Context({'name' : 'Yasmika'})
print(t.render(t))

I'm learning the basics of Python Django. When I'm trying to run Django template, Python interpreter gives me this error. How can I fix this?

C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/YasmikaSH/Desktop/lern_django/lern_django/test2.py
Traceback (most recent call last):
  File "C:/Users/YasmikaSH/Desktop/lern_django/lern_django/test2.py", line 3, in <module>
    t = template.Template("My name is {{ name }}.")
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py", line 184, in __init__
    engine = Engine.get_default()
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\engine.py", line 76, in get_default
    django_engines = [engine for engine in engines.all()
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 89, in all
    return [self[alias] for alias in self]
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 86, in __iter__
    return iter(self.templates)
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 29, in templates
    self._templates = settings.TEMPLATES
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
    self._setup(name)
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\conf\__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Colwin
  • 2,655
  • 3
  • 25
  • 25
yasmikash
  • 157
  • 2
  • 14
  • No. But I will not understand it even if I read it. – yasmikash May 19 '17 at 05:29
  • 2
    That's not a useful mentality when writing code... Django isn't really meant to be used outside of a Django project. You can configure it to work from within a single `.py` file, but it takes some effort. – Blender May 19 '17 at 05:31
  • Possible Duplicate http://stackoverflow.com/questions/15556499/django-db-settings-improperly-configured-error – Agam Banga May 19 '17 at 05:33
  • Possible duplicate of [ImproperlyConfigured: You must either define the environment variable DJANGO\_SETTINGS\_MODULE or call settings.configure() before accessing settings](http://stackoverflow.com/questions/26082128/improperlyconfigured-you-must-either-define-the-environment-variable-django-set) – Exprator May 19 '17 at 05:43

3 Answers3

2

I think you are trying

print(t.render(c))

Do python manage.py shell

In [74]: from django import template

In [75]: t = template.Template("My name is {{ name }}.")

In [76]: c = template.Context({'name' : 'Yasmika'})

In [77]: print(t.render(c))
My name is Yasmika.
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
1

Django isn't really meant to be used outside of a Django project (or something that looks like one). You can start a Django project by following the "Writing your first Django app" tutorial. Once you have a project setup, launch a shell by running:

$ python manage.py shell

This loads a Python shell with Django fully configured to use your settings.

Blender
  • 289,723
  • 53
  • 439
  • 496
0

It looks like you don't have your DJANGO_SETTINGS_MODULE setting defined in your environment.

Have a look here for the documentation related to the settings module -> https://docs.djangoproject.com/en/1.8/topics/settings/

Your can set it like this

export DJANGO_SETTINGS_MODULE=<yoursite>.settings

When you use Django, you have to tell it which settings you’re using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.

The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.

Colwin
  • 2,655
  • 3
  • 25
  • 25