>>> 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. Java关于时间日期的Date类和Calendar类概述

    1.      System.currentTimeMillis()方法 可以获取当前时间距离1970年01月01日00时00分00秒的秒数,如果程序运行在北京时区,则获取的数据是当前时间距离1970 ...

  2. React Native - 0序言

    1. 什么是React Native? React Native是Facebook在React.js Conf 2015大会上推出的一个用于开发Android t iOS App的一个框架.主要编程语 ...

  3. [libGDX游戏开发教程]使用libGDX进行游戏开发(12)-Action动画

    前文章节列表:  使用libGDX进行游戏开发(11)-高级编程技巧   使用libGDX进行游戏开发(10)-音乐音效不求人,程序员也可以DIY   使用libGDX进行游戏开发(9)-场景过渡   ...

  4. 解决nextjs部署到now上之后出现的“Unable to import module 'now__launcher'”错误

    解决nextjs部署到now上之后出现的“Unable to import module 'now__launcher'”错误 这个错误是由于在next.config.js中直接引用了withLess ...

  5. ionic 之环境的搭建

    在ionic的学习中,首先要做的就是环境的搭建,接下来我对从一开始环境的搭建做了记录,以及过程中遇到的问题. 1.安装环境 1.1 安装Node.js Node.js建议安装最新版本,下载地址:htt ...

  6. HDU 多校1.7

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  7. 数据排序 第三讲( 各种排序方法 结合noi题库1.10)

    说了那么多种排序方法了,下面就来先做几个题吧 06:整数奇偶排序 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 给定10个整数的序列,要求对其重新排序.排序 ...

  8. ( 转 ) Mysql group_concat 的反向应用实现(Mysql列转行)

    用过Mysql的都知道她有一个很好的实现行转列功能的函数group_concat函数,非常方便 点击(此处)折叠或打开 SELECT * FROM group_test; SELECT id, GRO ...

  9. Web应用渗透测试框架Arachni

    Web应用渗透测试框架Arachni   Arachni是一款Ruby语言编写的Web应用渗透测试框架.当用户指定目标后,该框架可以自动扫描网站页面,对页面中的链接.表单.Cookie.HTTP He ...

  10. python url拼接

    import datetime,requests import json,time,urllib import hashlib self.url_domain = “www.baidu,com/” u ...