iframe显示它无法连接。我尝试在视图上使用默认的@xframe_options_exempt装饰器,以及django-csp的@csp_exempt,但都没有效果。
给出的控制台错误如下:
Refused to display 'http://localhost:8000/new_pull/' in a frame because it set 'X-Frame-Options' to 'deny'.
和Failed to load resource: the server responded with a status of 404 (Not Found)
视图
@csp_exempt
@login_required
def new_pull(request):
"""Create a new pull request"""
if request.method != 'POST':
# No data submitted; create a blank form
form = PullForm()
else:
# POST data submitted; process data
form = PullForm(data=request.POST)
if form.is_valid():
new_pull = form.save(commit=False)
new_pull.owner = request.user
new_pull.save()
# Display a blank or invalid form.
context = {'form': form}
return render(request, 'learning_logs/new_pull.html', context)base.html
{% if user.is_authenticated %}
<br>
<iframe src="{% url 'learning_logs:new_pull' %}" title="Pull request Iframe"></iframe>
<iframe src="learning_logs/new_pull.html" title="Pull request Iframe"></iframe>
{% endif %}new_pull.html
<div class="pull container text-center border-top mt-5">
<h5 class="mt-2">Pull request</h5>
<p>New pull request:</p>
<form action="{% url 'learning_logs:new_pull' %}" method='post'>
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button name="submit" class="btn btn-green pl-2 pr-2">
<i class="fas fa-plus-circle"></i>
Create pull
</button>
{% endbuttons %}
<input type="hidden" name="next"
value="{% url 'learning_logs:bug_tracker' %}" />
</form>
</div>发布于 2020-05-27 15:42:09
尝试将其添加到Settings.py中
X_FRAME_OPTIONS = 'SAMEORIGIN'默认情况下,the X-Frame-Options设置为Deny
https://docs.djangoproject.com/en/3.0/ref/clickjacking/
这可能不是您的问题,因为有许多原因可能导致这种情况,如CSP。如果没有我的信息很难确定。
发布于 2022-10-22 02:35:09
首先很好地提到django-csp 配置django-csp的文档。
试着先做bones225提到的事情。请注意,您可以在Web工具中的响应头中检查所有当前的标题( X-Frame-Options "SAMEORIGIN';:-> Name ->单击html页面的->标题将在右侧打开)
您可能已经设置了指令CSP_DEFAULT_SRC = ("'self'"),而没有设置CSP_FRAME_SRC。然后也添加CSP_FRAME_SRC = ('localhost:8000')。
https://stackoverflow.com/questions/62027144
复制相似问题