使用django.template时出错 [英] Error when using django.template

查看:96
本文介绍了使用django.template时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是django的初学者,当使用django的模板模块时,我遇到了很多错误。
以下工作从python shell:

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?

推荐答案

您无法从python shell访问并运行django项目。 Django不知道你想要工作的项目。

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)

第一个是最简单和最好的方法。在django shell中运行代码。

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

这篇关于使用django.template时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆