【问题标题】:Nested variables in Django TemplatesDjango 模板中的嵌套变量
【发布时间】:2021-03-28 17:00:05
【问题描述】:

我不确定它是变量还是标签。

我有两个应用程序(不同的样式,不同的导航栏)但想从 db 打印同一个文档。

我创建了 base.html、带有扩展的模板,一切都运行良好。但是。

模板主体是从数据库中填充的,是html代码的一部分。

在这段代码中有<p>The contact email is: blabla@firstapp.example.com</p>

当然这对 secondapp 来说是错误的。

我尝试使用<p>The contact email is: {{ app_email }}</p> 更改正文,并使用上下文设置它

但它不起作用 - 它会打印

The contact email is: {{ app_email }}

模板/文档.html:

{% extends base %}
{% load crispy_forms_tags %}
{% load static %}

{% block head %}
<title>{{ title }}</title>
{% endblock %}

{% block content %}
<div class="container">
    <div class="row justify-content-center">
       <div class="col-9">
           <h1 class="mt-2">{{ document.header }} !!!</h1>
          <hr class="mt-0 mb-4">
          {% autoescape off %}
          {{ document.body }}
          {% endautoescape %}
       </div>
     </div>
</div>
{% endblock %}

firstapp.views.py:

def myview(request):
    context = {
            'app_title' : 'First',
            'title' : 'First - document',
            'app_name' : 'first',
            'app_email' : 'contact@first.example.com',
            'document' : get_document('document1'),
            'base' : 'first/base.html'

secondapp.views.py:

def myview(request):
    context = {
            'app_title' : 'Second',
            'title' : 'Second - document',
            'app_name' : 'second',
            'app_email' : 'contact@second.example.com',

            'document' : get_document('document1'),
            'base' : 'second/base.html'

这样可以吗?也许有一些过滤器?

已编辑: 现在我知道,我必须在 get_document 中预渲染它。但是如何传递未知参数呢?

此功能有效 - 但必须添加 sth do 参数 (*args? **kwargs?) 并重新定义 Context{{ *args? **kwargs?? }}

def get_document(name):
    doc = Doc.objects.get(name=name)
    doc.body = Template(doc.body).render(Context{{'app-email':'contact@first.example.com'}})
    return doc

【问题讨论】:

  • get_document('document1')结果中的

    电子邮件行吗?

  • @saquintes 完全正确

标签: django django-templates django-template-filters


【解决方案1】:

问题是您在这种情况下将document1 的内容视为上下文变量本身。它永远不会被 django 模板引擎解析,因此永远不会转换 {{ app_email }} 变量。

我看到两个选项:

  1. 如果文档是磁盘中的文件(根据您的描述,情况似乎并非如此),那么您需要弄清楚如何将文档作为另一个模板加载到您的模板中。我知道有标签可以根据变量的内容加载另一个模板。所以你会在你的视图中传递template_name = "template/path/to/document",然后在你的模板中包含{% include template_name %}之类的东西。实际上,即使模板不在磁盘上,您也可以编写一个模板加载器,从任何地方加载它。

  2. 或者,您可以通过模板引擎独立发送get_document(...) 的结果。因此,在 view.py 中,您将在将其添加到模板上下文之前单独渲染它。我认为曾经有一个 django.shortcuts.render_string 函数你可以通过它,尽管我认为这可能在较新的 Django 中有所改变。 更新来自 OP:Template(&lt;str&gt;).render(&lt;context&gt;) 是这样做的方法。

【讨论】:

  • 找不到 django.shortcuts.render_string 规范... :(
  • Template(doc.body).render(Context({'app_email':'first@example.com'})) - 它有效! - 但是如何将未知参数传递给 get_document 并在上下文中使用它?
【解决方案2】:

感谢@saquintes

def get_document(name,**kwargs):
    doc = Doc.objects.get(name=name)
    doc.body = Template(doc.body).render(Context(kwargs))
    return doc

在 first.views.py 中:

(...)

   def myview(request):
        context = {
            'app_title' : 'First',
            'title' : 'First - document',
            'app_name' : 'first',

            'document' : get_document('document1', app_email = 'contact@first.example.com'),
            'base' : 'first/base.html'

(...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 2010-12-26
    • 2012-10-25
    • 2014-03-04
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多