Django模板最简单的使用

本文详细介绍了如何在Django框架中正确使用模板系统,包括在Shell环境中避免的常见错误及正确的实践方法。通过实例演示了如何在PyCharm中设置并使用Django模板,以及如何在urls.py中设置相应的URL映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 包含静态和动态的内容,动态内容就是模板变量,在将模板发送给用户之前,需要将动态部分替换成相应的值

# 在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.

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值