Django模板中的嵌套变量

Django模板中的嵌套变量,django,django-templates,django-template-filters,Django,Django Templates,Django Template Filters,我不确定它是变量还是标记 我有两个应用程序(不同的样式,不同的导航栏),但我想从db打印相同的文档 我创建了base.html、带有extenses的模板,一切都很完美。但是 模板主体由数据库填充,数据库是html代码的一部分 在这段代码中,联系人的电子邮件是:blabla@firstapp.example.com 当然,这对于secondapp是错误的 我试图用更改正文,联系人电子邮件是:{{app_email},并用上下文设置它 但它不起作用——它会打印 The contact email

我不确定它是变量还是标记

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

我创建了base.html、带有extenses的模板,一切都很完美。但是

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

在这段代码中,联系人的电子邮件是:blabla@firstapp.example.com

当然,这对于secondapp是错误的

我试图用
更改正文,联系人电子邮件是:{{app_email}

,并用上下文设置它

但它不起作用——它会打印

The contact email is: {{ app_email }}
模板/document.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 %}
secondapp.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'
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_文档中预渲染它。但是如何传递未知参数呢

此函数可以工作-但必须添加sth-do参数(*args?**kwargs?) 并重新定义上下文{*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

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

我看到两种选择:

  • 如果文档是来自磁盘的文件(根据您的描述,情况似乎并非如此),那么您需要了解如何将文档作为另一个模板加载到模板中。我知道有一些标签用于根据变量的内容加载另一个模板。因此,您可以在视图中传递
    template\u name=“template/path/to/document”
    ,然后在模板中使用类似
    {%include template\u name%}
    的内容包含它。实际上,即使模板不在磁盘上,您也可以编写一个模板加载器,从任何地方加载它

  • 或者,您可以通过模板引擎独立发送
    get_document(…)
    的结果。因此,在view.py中,您可以在将其添加到模板上下文之前单独渲染它。我认为以前有一个
    django.shortcuts.render_string
    函数,你可以通过它,尽管我认为在较新的django中可能已经改变了从OP:
    Template()更新


  • 问题在于,在本例中,您将
    document1
    的内容本身视为上下文变量。django模板引擎永远不会解析它,因此
    {{{app_email}}
    变量永远不会转换

    我看到两种选择:

  • 如果文档是来自磁盘的文件(根据您的描述,情况似乎并非如此),那么您需要了解如何将文档作为另一个模板加载到模板中。我知道有一些标签用于根据变量的内容加载另一个模板。因此,您可以在视图中传递
    template\u name=“template/path/to/document”
    ,然后在模板中使用类似
    {%include template\u name%}
    的内容包含它。实际上,即使模板不在磁盘上,您也可以编写一个模板加载器,从任何地方加载它

  • 或者,您可以通过模板引擎独立发送
    get_document(…)
    的结果。因此,在view.py中,您可以在将其添加到模板上下文之前单独渲染它。我认为以前有一个
    django.shortcuts.render_string
    函数,你可以通过它,尽管我认为在较新的django中可能已经改变了从OP:
    Template()更新

  • 感谢@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',
                'app_email' : 'contact@first.example.com',
                'document' : get_document('document1'),
                'base' : 'first/base.html'
    
    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'
    
    (……)

    (…)

    感谢@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',
                'app_email' : 'contact@first.example.com',
                'document' : get_document('document1'),
                'base' : 'first/base.html'
    
    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('document1')
    结果中的电子邮件行?@saquintes确切地说是
    get_document('document1')
    结果中的电子邮件行?@saquintes准确地说无法找到django.shortcuts.render_字符串规范…:(模板(doc.body).render(上下文({'app_email':'first@example.com“}”)-有效!-但如何传递未知参数以获取_文档并在上下文中使用它?找不到django.shortcuts.render_字符串规范…:(模板(doc.body).render(上下文({'app_email':'first@example.com'}))-有效!-但如何传递未知参数以获取_文档并在上下文中使用它?