【问题标题】:Passing django view dictionary and variables to a iframe template in a template将 django 视图字典和变量传递给模板中的 iframe 模板
【发布时间】:2019-11-14 03:56:01
【问题描述】:

我有两页。

1) create_form.html ,使用 create_form 视图呈现 2) formatter.html ,用格式化程序视图呈现

我在 create_form.html 中使用 iframe 添加了格式化程序视图。

<iframe src = "/formatter" width = "768" height = "500" frameBorder="0"> </iframe>

我在渲染 create_form.html 时将字典传递给 create_form。我可以在 create_form 视图中找到这本字典。但我还需要将此字典传递给 create_form.html 中的 iframe 中的 formatter.html

【问题讨论】:

    标签: python html django django-templates django-views


    【解决方案1】:

    我找到了解决方案。我将字典如下传递给 iframed 视图。所以我能够在 iframed 模板中打印它。

    在 create_form.html 中:

    <iframe src = "{% url 'formatter' tester=tester %}" width = "768" height = "500" frameBorder="0"></iframe>
    

    在 Urls.py 中:

    url(r'^formatter/(?P<tester>[\w-]+)/$', formatter, name='formatter')
    

    在views.py中:

    @login_required(login_url='/accounts/login/')
    def formatter(request, tester=None):
        return render(request, "formatter.html", {'tester': tester})
    

    【讨论】:

    • 不适用于字典。上述解决方案仅适用于字符串等变量。我们可以传递变量,但不能在 iframe 中传递整个字典。
    【解决方案2】:

    您无法访问iframe 中的上下文变量;这是因为 iframe 是由单独的视图函数/类呈现的。

    但是你可以使用 django 的include template tag 来完成你想要的。

    如文档中所述:

    Include 加载模板并在当前上下文中呈现它。这是在模板中“包含”其他模板的一种方式。

    【讨论】:

    • 此解决方案在某些情况下可以工作。但我的问题是 2 个 html 页面有不同的 css 和 js 版本。如果我将它们包含在一个页面中,css 和 js 会在某些区域引起冲突。所以我试图用 iframe 保持分隔,但像一页一样显示它们。
    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 2019-04-03
    • 2014-11-30
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2015-06-22
    • 2012-09-27
    相关资源
    最近更新 更多