Error when using django.template

大憨熊 提交于 2019-12-03 03:49:22

问题


I'm a beginner in django and I got a lot of errors when using template module from django. The following works from the python shell:

from django import template
t = template.Template('My name is {{ name }}.')

When i use this code , i get the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/django/template/base.py", line 123, in __init__
 if settings.TEMPLATE_DEBUG and origin is None:
File "/usr/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/usr/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATE_DEBUG,but        
settings are not configured. You must either define the environment variable 
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Dose anyone have an idea about this error?


回答1:


You can't access and run django projects from python shell. Django doesn't know what project you want to work on.

You have to do one of these things:

1. python manage.py shell
2. Set DJANGO_SETTINGS_MODULE environment variable in your OS to mysite.settings
3. Use setup_environ in the python interpreter:
   from django.core.management import setup_environ
   from mysite import settings
   setup_environ(settings)

The first one is easiest and best method. Run your code in the django shell.




回答2:


If you want to use the template system without the django shell, you can do:

from django.conf import settings
settings.configure()

and after this your code

t = template.Template('My name is {{ name }}.')



回答3:


I also had the same problem.

The main point is that in the django book 2, this piece of code is not supposed for users to put into a Python IDLE started from your python.exe ( presumably in C:\Program Files\Python33 on Windows).

Rather, it is just showing how the piece of code will look like. The solution is simply run 'cmd' and change the directory to your 'mysite', and run

python manage.py shell

which ensure that the 'mysite.settings.py' is used.




回答4:


Of course the @Sudipta's answer works correctly but (just for the future) for the explanation you can also visit the "Creating Template Objects" -> "A special Python prompt" section in Django book




回答5:


To fix this issue, you need to run it in python manage.py shell instead of just python.

Reason: Well, many parts of Django rely on settings and you won't be able to use them until it knows which settings file to use. When you run it on python manage.py shell, Django knows which settings file to use and does the job for you.




回答6:


The error message states that you need to set the DJANGO_SETTINGS_MODULE variable. Running export DJANGO_SETTINGS_MODULE=mysite.settings, worked for me.



来源:https://stackoverflow.com/questions/18081369/error-when-using-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!