# 包含静态和动态的内容,动态内容就是模板变量,在将模板发送给用户之前,需要将动态部分替换成相应的值
# 在Shell中使用Django模板,不应该直接使用Python REPL
例如:这样会报错:
~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from django import template
>>> template.Template("My name is {{name}}.")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/toohoo/.local/lib/python3.6/site-packages/django/template/base.py", line 149, in __init__
engine = Engine.get_default()
File "/home/toohoo/.local/lib/python3.6/site-packages/django/template/engine.py", line 76, in get_default
for engine in engines.all():
File "/home/toohoo/.local/lib/python3.6/site-packages/django/template/utils.py", line 90, in all
return [self[alias] for alias in self]
File "/home/toohoo/.local/lib/python3.6/site-packages/django/template/utils.py", line 87, in __iter__
return iter(self.templates)
File "/home/toohoo/.local/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/toohoo/.local/lib/python3.6/site-packages/django/template/utils.py", line 28, in templates
self._templates = settings.TEMPLATES
File "/home/toohoo/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
self._setup(name)
File "/home/toohoo/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
应该在Django的shell里面使用模板:
操作如下:
toohoo@ubuntu:~/PycharmProjects/SecondDjango$ python manage.py shell
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django import template
>>> t = template.Template('My name is {{name}}.') #stept1
>>> c = template.Context({'name':'Bill'}) # stept2
>>> t.render(c) #stept3
u'My name is Bill.'
在Pycharm里面使用的方法是:
首先是设置模板:
MyTemplate.py
from django.http import HttpResponse from django import template def simpleTemplate(request): t = template.Template('My name is {{name}}.') c = template.Context({'name': 'Bill'}) return HttpResponse(t.render(c))
然后在urls.py里面设置映射路径
from django.conf.urls import url from django.contrib import admin from .views import First from .views import ServerTime from .views import Dynamicurl from .views import MyTemplate # 动态URL的使用 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^hh/',First.index), url(r'^time/$',ServerTime.currentDateTime), url(r'^$',ServerTime.currentDateTime), url(r'^time1$',ServerTime.currentDateTime), #url(r'^fun/1$',Dynamicurl.fun1), #url(r'^fun/2$',Dynamicurl.fun2), #url(r'^fun/3$',Dynamicurl.fun3), # 映射变成正则表达式 url(r'^fun/(\d{1,3})/$',Dynamicurl.fun), url(r'^simpleTemplate/$',MyTemplate.simpleTemplate) ]
然后确定启动服务器,访问http://127.0.0.1:8000/simpleTemplate/
就可以得到:My name is Bill.