AJAX调用Django视图的内部错误(restframework endpoint) [英] Internal error on AJAX call to a Django view (restframework endpoint)
本文介绍了AJAX调用Django视图的内部错误(restframework endpoint)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
jQUERY:
$.ajax({
url: '/notify/',
type:'GET',
dataType: 'json',
success: function (data) {
if (data.is_taken) {
alert("A user with this username already exists.");
}
}
});
urls.py:
url(r'^notify/$', views.notify,name='notify'),
views.py:
from django.http import HttpResponse
from django.http import Http404
from django.http import JsonResponse
def notify(request):
data = {
'is_taken': Notification.objects.all()
}
return JsonResponse(data)
在控制台中调用ajax:
On calling ajax in console:
Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)
可能有什么问题?一切都是正确和现实的。 Django 1.8,jQuery 3.1.0
What may be wrong? Everything is correct and present . Django 1.8 , jQuery 3.1.0
编辑:Django错误日志追溯
Django error log traceback
Internal Server Error: /notify/
Traceback (most recent call last):
File "/Users/TheKotik/djboy/denv/lib/python3.5/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/TheKotik/djboy/blog/views.py", line 212, in notify
return JsonResponse(data)
File "/Users/TheKotik/djboy/denv/lib/python3.5/site-packages/django/http/response.py", line 535, in __init__
data = json.dumps(data, cls=encoder)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 198, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 256, in iterencode
return _iterencode(o, 0)
File "/Users/TheKotik/djboy/denv/lib/python3.5/site-packages/django/core/serializers/json.py", line 112, in default
return super(DjangoJSONEncoder, self).default(o)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/encoder.py", line 179, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: [<Notification: Notification object>, <Notification: Notification object>, <Notification: Notification object>, <Notification: Notification object>, <Notification: Notification object>, <Notification: Notification object>, <Notification: Notification object>, <Notification: Notification object>, <Notification: Notification object>] is not JSON serializable
推荐答案
你需要确保你r结果(在您的情况下数据
)是json可序列化,请注意您正在执行'is_taking':Notification.objects.all()
,你应该这样做:
You need to be sure your result (in your case data
) is json serializable, note you are doing 'is_taking': Notification.objects.all()
, you should do something like this instead:
from rest_framework.response import Response
from rest_framework.decorators import api_view
@api_view(['GET', ])
def notify(request):
data = {
'is_taken': NotificationSerializer(Notification.objects.all(), many=True).data
}
return Response(data)
并写下 Serializer eg NotificationSerializer
通知
这篇关于AJAX调用Django视图的内部错误(restframework endpoint)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文