1

我正在尝试使用用户在 Geodjango 中的位置创建 google map API 3 标记。

Internal Server Error 500我在渲染谷歌地图和标记时遇到。在 CMD 控制台上,我得到TypeError: 'GeoQuerySet' object is not callable.

您能否提出解决方案并解释异常的原因?

views.py

class MyView(View):
    def get(self, request):
        resp = UserList.objects.all().values('nickname', 'last_location')
        print (resp)
        return JsonResponse(request, resp, safe=False)  

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from mymap_app import views as map_views

admin.autodiscover()
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^my_endpoint/$',map_views.MyView.as_view(),name='user_list'),
    url(r'^$',map_views.IndexView.as_view(), name='index'),

    ]

追溯:

Traceback (most recent call last):
  File "C:\Python35-32\lib\site-packages\django\core\handlers\exception.py", lin
e 41, in inner
    response = get_response(request)
  File "C:\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187
, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185
, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Python35-32\lib\site-packages\django\views\generic\base.py", line 68,
 in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Python35-32\lib\site-packages\django\views\generic\base.py", line 88,
 in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\Kaleab\Desktop\WebMap\AAmap_Project\mymap_app\views.py", line 2
1, in get
    return JsonResponse(request, resp, safe=False)
  File "C:\Python35-32\lib\site-packages\django\http\response.py", line 530, in
__init__
    data = json.dumps(data, cls=encoder, **json_dumps_params)
  File "C:\Python35-32\lib\json\__init__.py", line 237, in dumps
    **kw).encode(obj)
TypeError: 'GeoQuerySet' object is not callable
[05/Sep/2017 22:36:45] "GET /my_endpoint/ HTTP/1.1" 500 18865
4

2 回答 2

0

看看这个:data = json.dumps(data, cls=encoder, **json_dumps_params)

JsonResponse 正在尝试序列化您传递给它的参数,但 GeoQuerySet 无法通过函数调用进行序列化,因此会引发 TypeError。请参阅以下文档: https ://docs.python.org/2/library/json.html

如果 skipkeys 为 true(默认值:False),则将跳过非基本类型(str、unicode、int、long、float、bool、None)的字典键,而不是引发 TypeError。

为了解决这个问题,您可以这样做:

return HttpResponse(
    json.dumps(response, skipkeys=True), content_type="application/json"
)

也可以将 GeoQuerySet 格式化为 python 字典,然后将其传递给 JsonResponse。

于 2017-09-05T23:42:38.300 回答
0

对views.py 的以下更改解决了该Geoqueryset object not callable错误。

class MyView(View):
    def get(self, request):
        response = UserList.objects.all().values('nickname','last_location')
        print(response)
        return JsonResponse({'response':list(response)})
于 2017-09-12T12:56:59.293 回答