Django——模版Template报错

>>> 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

[email protected]:~/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报错,布布扣,bubuko.com

时间: 2024-10-18 21:42:29

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

记一次django的诡异报错 Could not parse the remainder: '=' from '='

如题,一个展示日志的功能,调用该模板文件时报错,Could not parse the remainder: '=' from '='.这行模板语言在上面出现过同样的,仅仅是改了'<='右端的整数大小,而且该有的标签也不少. 百般思考无解,stackoverflow和百度都没答案.后来重新看了下开发文档,发现是Django的版本不对,开发时用的1.8.2,我部署时用的1.11.5.重新安装低版本的django后问题解决.但原因仍待查. 记一次django的诡异报错 Could not parse

Django urls.py报错: raise TypeError(&#39;view must be a callable or a list/tuple in the case of include()

Django urls.py报错: raise TypeError('view must be a callable or a list/tuple in the case of include() 报错信息 "F:\PyCharm 2016.2.2\bin\runnerw.exe" C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe F:/Django/blogs/manage.py run

kendo模板 Uncaught Error: Invalid template:&#39; 报错

I was having a problem with a grid toolbar template because of a # in a hrefWorked out that I needed to excape the # with \\\ and not \\ <script type="text/x-kendo-template" id="toolbarTemplate">     <div class='toolbar'>  

django admin 中文报错incorrect string value解决方案

对于错误" Incorrect string value: '\xE6\xA2\xB5\xE8\x92\x82...'for column 'object_repr' at row 1 解决方法是设置django_admin_log表的object_repr一项使用utf8_unicode_ci: 对于错误"  Incorrect string value: '\xE6\xA2\xB5\xE8\x92\x82...'for column 'change_message' at row

Django 使用allauth报错 RuntimeError: Model class django.contrib.sites.models.Site doesn&#39;t declare an explicit app_label and isn&#39;t in an application in INSTALLED_APPS

一:报错 RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS 出现这种情况需将Django的Sites框架添加到您的应用程序中,并在您的设置中将SITE_ID设置为1即可.位置放在默认配置的后面,其他应用的前面. INSTALLED_APPS = [ 'django

Django:django-cors-headers 报错no module named &quot;corsheaders&quot;

django跨域使用 pip install django-cors-headers 然后在settings文件中加上参数设置 # app配置 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles

django使用search_fields报错

django使用search_fields报错(in get_lookup_constraint) FieldError at /api/workorder/order/ Related Field got invalid lookup: icontains Request Method: GET Request URL: http://127.0.0.1:8000/api/workorder/order/?search=1 Django Version: 2.0 Exception Type:

pycharm无法新建django项目,报错timed out,需先建立pure项目,进入后建立django项目,最后调整目录层级。

一.pycharm无法新建django项目,报错timed out,需先建立pure项目,名字改成mysite: 二.进入后,在下方Terminal里,输入: pip install Django==2.2.7 用来安装指定版本的django 三.继续在下方Terminal里,输入: django-admin startproject mysite 创建一个名叫mysite的django项目. 四.执行后,你会发现目录结构有变化 点击右边的add configuration,然后点击弹出来窗口的

Django 中文注释报错问题

views.py 前面一定要加# -*- coding: utf-8 -*-否则报错 # -*- coding: utf-8 -*- from django.shortcuts import render_to_response def index(req):     return render_to_response('index.html',{}) # 第一种方式 # 回送模板(第一个return),回送标题(req下直接return字符串) # from django.http impor