在iframe中显示.html文件,使用django上传为媒体文件
我希望在我的模板上的iframe中显示上传.html
文件。我宁愿不将其设置为可以在urls.py
文件中访问的URL,我将.html
Files从管理面板上升至媒体文件夹,如So:
Modal
# Portfolio project overview model
class Work(models.Model):
html = models.FileField(upload_to="work_app/media/htmls/", null=True, blank=True)
settings.py
MEDIA_ROOT = str(BASE_DIR) + "/media/"
MEDIA_URL = '/media/'
STATIC_ROOT = str(BASE_DIR) + "/static/"
STATIC_URL = '/static/'
urls.py
urlpatterns = [
include("personal_portfolio_project.apps.resume_app.urls")),
path("work/", include("personal_portfolio_project.apps.work_app.urls")),
]
if DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我第一次尝试使用此template.html
代码:
{% extends "base.html" %}
{% load static %}
{% block page_content %}
<h1>{{ work.title }}</h1>
<div class="row">
<div class="col-md-8">
{% if work.html %}
<iframe height="100%" width="100%" src="{{ 'work.html.url' }}">
</iframe>
{% endif %}
</div>
</div>
{% endblock %}
...看起来像这样的尝试:
如您所见就像它找不到.html文件(404)一样。
我在其他几篇文章中看到了HTML行应该是:
<iframe height="100%" width="100%" src="{% url 'work.html' %}"> </iframe>
..和还添加x_frame_options ='Sameorigin'
to settings.py
file,所以我都做了那些,我现在得到的那些地方:
这是什么要告诉我的?我想念什么?
更新 我还尝试了:
&lt; iframe height =“ 100%” width =“ 100%” src =“ {{work.html}}}”&gt; &lt;/iframe&gt;
我再获得404:
更新
我获得的最接近的是:
&lt; iframe type =“ html”高度=“ 100%” width =“ 100%” src =“ {{work.html.url}}}}”&gt; &lt;/iframe&gt;
,但它给了我这个屏幕:
我将URL放在图像上方,以确保其实际上是正确的。根据其他答案,他们说只是为了启用x_frame_options ='Sameorigin'
,它将起作用,但这对我而言并不适用。这里的任何帮助将不胜感激。谢谢!
I'm looking to display an uploaded .html
file in an iFrame on my template. I would rather NOT set it up as a url to visit in the urls.py
file, I upload .html
files from the admin panel to the media folder like so:
model
# Portfolio project overview model
class Work(models.Model):
html = models.FileField(upload_to="work_app/media/htmls/", null=True, blank=True)
settings.py
MEDIA_ROOT = str(BASE_DIR) + "/media/"
MEDIA_URL = '/media/'
STATIC_ROOT = str(BASE_DIR) + "/static/"
STATIC_URL = '/static/'
urls.py
urlpatterns = [
include("personal_portfolio_project.apps.resume_app.urls")),
path("work/", include("personal_portfolio_project.apps.work_app.urls")),
]
if DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
My first attempt to display it using this template.html
code:
{% extends "base.html" %}
{% load static %}
{% block page_content %}
<h1>{{ work.title }}</h1>
<div class="row">
<div class="col-md-8">
{% if work.html %}
<iframe height="100%" width="100%" src="{{ 'work.html.url' }}">
</iframe>
{% endif %}
</div>
</div>
{% endblock %}
...looks like this:
As you can see, the iFrame is displaying, but seems like it can't find the .html file (404).
I saw in a few other posts that the html line should be:
<iframe height="100%" width="100%" src="{% url 'work.html' %}"> </iframe>
..and to also add X_FRAME_OPTIONS = 'SAMEORIGIN'
to your settings.py
file, so I did both those, where I now get:
What is this trying to tell me? What am I missing?
UPDATE
I have also tried:
<iframe height="100%" width="100%" src="{{ work.html }}"> </iframe>
to which I get another 404:
UPDATE
The closest I've gotten is using this:
<iframe type="html" height="100%" width="100%" src="{{ work.html.url }}"> </iframe>
But it gives me this screen:
I put the url above the image to make sure that it is in fact correct. According to other answers, they said just to enable X_FRAME_OPTIONS = 'SAMEORIGIN'
and it'll work, but it doesn't for me. Any help from here would be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我收回上面的最后一个更新,我要做的就是清除缓存,或以隐身模式运行页面,因为由于缓存,我的更改都没有在页面上反映在页面上。一旦我这样做,它就会开始正确显示iframe。
我正在探索的另一个选择是将Xframe豁免装饰器设置为处理呈现帖子详细信息页面的视图,看起来像是这样的:
似乎也有效,但我正在恢复到Sameorigin方法。
I take back my last UPDATE above, what I had to do was clear my cache, or run the page in incognito mode as none of my changes were being reflected on the page due to the cache. Once I did that, it started showing the iframe properly.
Another option I was exploring was to set an xframe exemption decorator to the view that handles rendering the post details page, which would look something like:
which also seemed to work, but I'm reverting back to the SAMEORIGIN method.