AttributeError: 'ModelName' object has no attribute 'get' if response.get("X-Frame-Options") is not None:
AttributeError: 'ModelName' object has no attribute 'get' if response.get("X-Frame-Options") is not None:
我正在尝试获取脚本列表
我不确定为什么这个错误是 django 抛出的但是,任何人都可以确定是否有问题。
型号
class Script(models.Model):
name = models.CharField(max_length=100)
script = models.TextField()
def __str__(self):
return self.name
序列化器
class ScriptSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(read_only=True)
script = serializers.CharField(read_only=True)
查看;
@api_view(['GET'])
def scripts(request):
scripts = Script.objects.all()
serializer = ScriptSerializer(scripts, many=True)
return Response(serializer.data)
但是当我调用这个视图时,我得到这个错误
Internal Server Error: /api/v1/seo/scripts
Traceback (most recent call last):
File "E:\folder\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "E:\folder\venv\lib\site-packages\django\utils\deprecation.py", line 136, in __call__
response = self.process_response(request, response)
File "E:\folder\venv\lib\site-packages\django\middleware\clickjacking.py", line 27, in process_response
if response.get("X-Frame-Options") is not None:
AttributeError: 'Script' object has no attribute 'get'
我也在 Whosebug 中看到了与该问题相关的答案,但无法重现该解决方案。而且这种方法对我来说并不新鲜,我一直在整个项目中使用这种类型的功能。但我现在很惊讶为什么会这样。
编辑:
进口
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializers import ScriptSerializer
from .models import Script
网址
from django.urls import path
from . import views as views
app_name = "seo"
urlpatterns = [
path('scripts', views.Script, name='scripts'),
]
我认为这里的问题在于命名冲突。仔细看看这个:
@api_view(['GET'])
def scripts(request):
scripts = Script.objects.all()
serializer = ScriptSerializer(scripts, many=True)
名为 scripts
的函数,然后用 scripts = Script.objects.all()
重新分配了此名称。这可能会导致真正意想不到的行为
因此请尝试重命名您的局部变量。即:
@api_view(['GET'])
def scripts(request):
script_items = Script.objects.all()
UPD 在 urls.py
添加后:
它应该指向函数视图i.o。型号:
path('scripts', views.scripts, name='scripts'),
我正在尝试获取脚本列表
我不确定为什么这个错误是 django 抛出的但是,任何人都可以确定是否有问题。
型号
class Script(models.Model):
name = models.CharField(max_length=100)
script = models.TextField()
def __str__(self):
return self.name
序列化器
class ScriptSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(read_only=True)
script = serializers.CharField(read_only=True)
查看;
@api_view(['GET'])
def scripts(request):
scripts = Script.objects.all()
serializer = ScriptSerializer(scripts, many=True)
return Response(serializer.data)
但是当我调用这个视图时,我得到这个错误
Internal Server Error: /api/v1/seo/scripts
Traceback (most recent call last):
File "E:\folder\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "E:\folder\venv\lib\site-packages\django\utils\deprecation.py", line 136, in __call__
response = self.process_response(request, response)
File "E:\folder\venv\lib\site-packages\django\middleware\clickjacking.py", line 27, in process_response
if response.get("X-Frame-Options") is not None:
AttributeError: 'Script' object has no attribute 'get'
我也在 Whosebug 中看到了与该问题相关的答案,但无法重现该解决方案。而且这种方法对我来说并不新鲜,我一直在整个项目中使用这种类型的功能。但我现在很惊讶为什么会这样。
编辑: 进口
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializers import ScriptSerializer
from .models import Script
网址
from django.urls import path
from . import views as views
app_name = "seo"
urlpatterns = [
path('scripts', views.Script, name='scripts'),
]
我认为这里的问题在于命名冲突。仔细看看这个:
@api_view(['GET'])
def scripts(request):
scripts = Script.objects.all()
serializer = ScriptSerializer(scripts, many=True)
名为 scripts
的函数,然后用 scripts = Script.objects.all()
重新分配了此名称。这可能会导致真正意想不到的行为
因此请尝试重命名您的局部变量。即:
@api_view(['GET'])
def scripts(request):
script_items = Script.objects.all()
UPD 在 urls.py
添加后:
它应该指向函数视图i.o。型号:
path('scripts', views.scripts, name='scripts'),