zoukankan      html  css  js  c++  java
  • Django——模版Template报错

    >>> from django.template import Template
    >>> t = Template("My name is {{ my_name }}.")
    Traceback (most recent call last):
      File "C:Python33libsite-packagesdjangoconf\__init__.py", line 38, in _setup
        settings_module = os.environ[ENVIRONMENT_VARIABLE]
      File "C:Python33libos.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:Python33libsite-packagesdjango emplatease.py", line 123, in __init__
        if settings.TEMPLATE_DEBUG and origin is None:
      File "C:Python33libsite-packagesdjangoconf\__init__.py", line 54, in __getattr__
        self._setup(name)
      File "C:Python33libsite-packagesdjangoconf\__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
  • 相关阅读:
    C# 遍历类字段 及 字段值
    u8 采购到货单中的 业务类型 没有表字典,是系统预置 存入表也是文字: 普通采购 固定资产
    win7 X64 OLEDB读取EXCEL提示“未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序”
    关于 C# select 读取 Excel 数据 为空,或成科学计数法解决方案
    如何设置C#程序在Windows 7 Vista下以管理员权限运行
    C# OLE DB 的连接方式下 对 sql 参数的操作 需要改成 ? 号
    NHibernate (一) 五部曲
    做优秀产品经理
    Python Test in Hudson
    [转载]Openstack Essex安装文档资料
  • 原文地址:https://www.cnblogs.com/Simon-xm/p/3890752.html
Copyright © 2011-2022 走看看