一、概述

想要定制或者扩展模版引擎,模版系统工作原理,自动转移特征

名词解析:模板 渲染 就是是通过从context获取值来替换模板中变量并执行所有的模板标签。

二、Context处理器

如果在模版中经常使用相同的模版变量,这是会产生大量的代码冗余,可以通过RequestContext来解决这个问题。

例如:

from django.template import loader, Context

def view_1(request):
    # ...
    t = loader.get_template('template1.html')
    c = Context({
        'app': 'My app',
        'user': request.user,
        'ip_address': request.META['REMOTE_ADDR'],
        'message': 'I am view 1.'
    })
    return t.render(c)

def view_2(request):
    # ...
    t = loader.get_template('template2.html')
    c = Context({
        'app': 'My app',
        'user': request.user,
        'ip_address': request.META['REMOTE_ADDR'],
        'message': 'I am the second view.'
    })
    return t.render(c)
View Code

相关文章:

  • 2021-11-03
  • 2021-12-27
  • 2022-12-23
  • 2021-11-26
  • 2022-01-23
  • 2021-12-20
  • 2022-02-02
  • 2021-09-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2021-11-16
  • 2021-11-28
相关资源
相似解决方案