>>> from django.template import Template
>>> t = Template("My name is {{ my_name }}.")
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\django\conf\__init__.py", line 38, in _setup
    settings_module = os.environ[ENVIRONMENT_VARIABLE]
  File "C:\Python33\lib\os.py", line 676, in __getitem__
    raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'
 
During handling of the above exception, another exception occurred:
 
Traceback (most recent call last):
  File "", line 1, in
    t = Template("My name is {{ my_name }}.")
  File "C:\Python33\lib\site-packages\django\template\base.py", line 123, in __init__
    if settings.TEMPLATE_DEBUG and origin is None:
  File "C:\Python33\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:\Python33\lib\site-packages\django\conf\__init__.py", line 47, 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.
 
解决方法一:

先导入settings

>>> from django.conf import settings

>>> settings.configure()

>>> from django import template
>>> t = template.Template('My name is {{ name
}}.')
>>> c = template.Context({'name':
'yixiaohan'})
>>> print t.render(c)
My name is yixiaohan.
>>> c = template.Context({'name':
'xiaowangge'})
>>> print t.render(c)
My name is xiaowangge.

解决方法二:

使用python manage.py shell启动
Python交互式解释器(实际上启动的是Ipython)

python manage.py shell

yixiaohan@ubuntu:~/djbk$ python manage.py shell
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
Type "copyright", "credits" or "license" for more
information.

IPython 0.12.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's
features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra
details.

In [1]: from django import template

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

In [3]: c = template.Context({'name':'yixiaohan'})

In [4]: rt = t.render(c)

In [5]: rt
Out[5]: u'my name is yixiaohan'

In [6]: print rt
my name is yixiaohan

Django——模版Template报错的更多相关文章

  1. django startproject xxx:报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 13: ordinal not in range(128)

    django startproject xxx:报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 13: o ...

  2. Django迁移数据库报错

    Django迁移数据库报错 table "xxx" already exists错误 django在migrate时报错django migrate error: table 'x ...

  3. org.thymeleaf.exceptions.TemplateInputException: Error resolving template 报错

    org.thymeleaf.exceptions.TemplateInputException: Error resolving template报错 遇到二次,第一次是刚刚学的时候,都是一个原因,而 ...

  4. django正常运行确报错的解决方法

    django正常运行却报错的处理方法 出处 : https://www.infvie.com/ops-notes/django-normal-operation-error 报错一:self._soc ...

  5. Django:django-cors-headers 报错no module named "corsheaders"

    django跨域使用 pip install django-cors-headers 然后在settings文件中加上参数设置 # app配置 INSTALLED_APPS = [ 'django.c ...

  6. django调用py报错 django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured.

    完整报错信息如下 django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, bu ...

  7. django 连接mysql报错

    原因: 问题1. 即从mysql5.7版本之后,默认采用了caching_sha2_password验证方式. 问题2.  然后在执行 python manage.py makemigrations依 ...

  8. Django创建App报错

    在django下创建APP项目时遇到的坑 python manage.py startapp app01 报错内容如下: 解决:找到报错中的文件夹151行删除items(),)中的逗号即可 在命令行下 ...

  9. Django 使用allauth报错

    一:报错 RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_labe ...

随机推荐

  1. mysql-5.7.15-winx64配置

    1.    配置环境变量 1.1  添加path路径 选择 控制面板>系统和安全>系统>高级系统设置>环境变量 mysql文件目录的绝对路径\bin 1.2  修改mysql ...

  2. redis使用管道pipeline提升批量操作性能(php演示)

    Redis是一个TCP服务器,支持请求/响应协议. 在Redis中,请求通过以下步骤完成: 客户端向服务器发送查询,并从套接字读取,通常以阻塞的方式,用于服务器响应. 服务器处理命令并将响应发送回客户 ...

  3. mySQL的存储过程详解

    mysql存储过程详解 1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...

  4. scrapy snippet

    1. spider文件 from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors. ...

  5. HDU 1556 Color the ball【差分数组裸题/模板】

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...

  6. 主席树 STL+二分【p3939】数颜色

    Description 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 \(1\) 到 \(n\) 的 \(n\) 只兔子排成长长的一 ...

  7. 【贪心】bzoj3721 PA2014 Final Bazarek

    考虑不限制奇偶的情况,那就是直接排序取前k个的和. 加上奇偶限制:若排序后的前k个的和是偶数,则“显然地”:将其中的最小的奇数替换成未被选择的数中最大的偶数 或者 将其中的最小的偶数替换成未被选择的数 ...

  8. 显示图案 Exercise06_06

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:显示图案 * 输入一个数 5 1 2 1 3 2 1 4 3 2 1 5 ...

  9. Problem B: 零起点学算法17——2个数比较大小

    #include<stdio.h> int main() { int n,m; while(scanf("%d %d",&n,&m)!=EOF) if( ...

  10. 制作推送证书 and Code=3000 "未找到应用程序的“aps-environment”的权利字符串"

    制作推送证书 step1. 打开苹果开发者网站 step2. 从Member Center进入Certificates, Identifiers & Profiles step3. 选择要制作 ...