简体   繁体   中英

Django Jsonresponse filtered queryset by id

My objective is to get the id and make a queryset filtered by id, as in the following code:

views.py

class MyProfile(TemplateView):
model = Reports
template_name = 'template.html'
def get_context_data(request, *args, **kwargs):
    if kwargs.get('pk', None):
        q = kwargs.get('pk', None)
        queryset = Reports.objects.all().values('id','line_x','line_y',).filter(id = q)
        data = list(queryset)
        return JsonResponse(data, safe=False)

urls.py

url(r'^profiles/(?P<pk>\d+)/$', views.MyProfile.as_view())

It returns the following error:

context must be a dict rather than JsonResponse

Django 1.11.8

from django.http import JsonResponse

def different_function_name(request, *args, **kwargs):
    if kwargs.get('pk', None):
       q = kwargs.get('pk', None)
       queryset = Reports.objects.all().values('id','line_x','line_y',).filter(id = q) 
       query_list = list(queryset)
       return JsonResponse(query_list, safe=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM