首页 > 解决方案 > 在 Django 管理视图中显示 iframe 元素

问题描述

我一直在尝试向 Django 的管理站点添加一个简单的视图,就像这样

# collection_management/admin.py

    class MyAdminSite(admin.AdminSite):
    ...

    def plasmid_map_view(self, request):
        return render(request, 'admin/collection_management/plasmid_map.html')
    ...

它使用一个模板,其中包含一个加载外部 url 的 iframe 元素

# templates/admin/collection_management/plasmid_map.html

...

{% block content %}<div id="content-main">
<iframe 
style="height: 80vh; width: 100%; resize: both; overflow: auto; border:None;"
src="​https://designer.genomecompiler.com/plasmid_iframe?file_url=http://s3.amazonaws.com/gcc_production/plasmid_viewer/OG34_OG34_pSF-OXB19.gb​">
</iframe>
</div>
{% endblock %}

...

当我尝试加载页面时,我得到一个奇怪的行为,即 Django 将我的网站(example.net:8443)的“根”url 附加到应该在 iframe 内打开的地址,这显然不存在

https://example.net:8443/collection_management/plasmid_map/%E2%80%8Bhttps:/designer.genomecompiler.com/plasmid_iframe?file_url=http://s3.amazonaws.com/gcc_production/plasmid_viewer/OG34_OG34_pSF-OXB19.gb%E2%80%8B

在此处输入图像描述

我怎样才能阻止 Django 这样做?

标签: djangodjango-templatesdjango-views

解决方案


src 字符串两侧有一些隐藏字符


推荐阅读