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

查看:30
本文介绍了使用 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.

有人知道这个错误吗?

推荐答案

您无法从 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天全站免登陆