【问题标题】:Django include template tag alternative in python classDjango在python类中包含模板标签替代
【发布时间】:2015-08-31 10:57:40
【问题描述】:

我知道有一个 Django 模板标签,它允许像这样包含其他模板:

{% include 'template.html' %}

我想知道我是否可以这样做,但是在一个单独的 python 类中(不在 HTML 模板中)?

我知道例如 url 标签有一个等价物,像这样:

{% url 'some_url_name' %} -> reverse('some_url_name')

【问题讨论】:

  • 你在一个单独的python类中是什么意思?你的例子不清楚
  • @petkostas 我的意思是我可以做同样的事情,但不是在 HTML 模板中,因为 HTML 模板使用不同的符号来实现同样的事情。

标签: python django django-templates


【解决方案1】:

我想你可能正在寻找render_to_string

from django.template.loader import render_to_string
context = {'foo': 'bar'}
rendered_template = render_to_string('template.html', context)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 2013-06-05
    • 2015-02-03
    • 2016-08-12
    • 2018-02-22
    • 2013-02-13
    • 2017-01-05
    相关资源
    最近更新 更多