首页 > 解决方案 > tuple' 对象没有属性 'get'

问题描述

这是 django 为我提供的回溯。

if getattr(response, 'xframe_options_exempt', False):
    return response

response['X-Frame-Options'] = self.get_xframe_options_value(request,response)
return response

def get_xframe_options_value(self, request, response):

获取要为 X_FRAME_OPTIONS 标头设置的值。使用 X_FRAME_OPTIONS 设置中的值,如果未设置,则使用“拒绝”。

如果需要,可以覆盖此方法,允许它根据请求或响应而变化。

return getattr(settings, 'X_FRAME_OPTIONS', 'DENY').upper()

我很难弄清楚为什么会发生这个错误。如何找出该元组在我的代码中的位置?这是一个应用程序的一个视图

from django.shortcuts import render
from django.http import HttpResponse
from .models import destination
# Create your views here.
def index(request):

    dests=destination.objects.all()

    return render(request,'index.html',{'dests':dests}),

现在这是第二个视图

from django.http import HttpResponse
from django.shortcuts import render
def HOMEE(request):
    return render(request,'HOMEE.html'),

标签: pythondjango

解决方案


正如威廉在评论中所说,您必须删除尾随逗号:

return render(request,'index.html',{'dests':dests}),

这背后的原因是在 python 中,您只需编写用逗号分隔的不同对象即可创建元组。所以

return render(request,'index.html',{'dests':dests}),

实际上等于return (render(request,'index.html',{'dests':dests}), )

您可以在控制台中轻松尝试:

在此处输入图像描述


推荐阅读