为什么Django网站没有嵌入到另一个HTML中? - iframe?

时间:2017-09-06 16:41:12

标签: python html django iframe

我尝试在另一个html页面中嵌入django表单,但没有任何效果。我尝试了我的其他django网站。但不起作用。还测试了一些其他网站。在iframe中使用django块吗?如何使其工作? 表格需要嵌入Programming competition form

模板:

<form method="post">
  {% csrf_token %}
  <b>{{form.as_p}}</b>
  <input type="submit" value="Submit" title="Submit" />
</form>

尝试嵌入为:

<html>
<iframe frameborder="1" src="http://form.classof20.cf/Programming_Competition/"></iframe>
</html>

它给了一个边框而没有任何内部。

1 个答案:

答案 0 :(得分:2)

尝试加载HTML后,webkit检查器中出现以下错误:

Refused to display 'http://form.classof20.cf/Programming_Competition/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.
Failed to load resource: net::ERR_BLOCKED_BY_RESPONSE

实际上,这是curl的响应标头转储:

$ curl -I http://form.classof20.cf/Programming_Competition/

HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Wed, 06 Sep 2017 19:44:16 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 765
Connection: keep-alive
Vary: Cookie
X-Frame-Options: SAMEORIGIN
Set-Cookie: csrftoken=UJZltdTzJMe6961QMNRSgZ7vKWa1vUEf2lEB8lmaaZXgROf1zyALsuwsKpvtcby6; expires=Wed, 05-Sep-2018 19:44:16 GMT; Max-Age=31449600; Path=/

那么,它来自哪里?它来自Django clickjacking protection

解决方案0:确保您的django响应allows your other site in X-Frame-Options,即:

X-Frame-Options: ALLOW-FROM http://your-other-site-which-embeds/

解决方案1:从单击劫持保护中豁免您的表单视图:

  

使用中间件时,可能会有一些您不使用的视图   想要X-Frame-Options标头集。对于这些情况,您可以使用   视图装饰器告诉中间件不要设置标题:

from django.http import HttpResponse
from django.views.decorators.clickjacking import xframe_options_exempt

@xframe_options_exempt
def ok_to_load_in_a_frame(request):
    return HttpResponse("This page is safe to load in a frame on any site.")