Unable to display request using ajax

I cannot display the request when using ajax

here is mine views.py:

if request.user.is_authenticated():
    productid = request.GET.get('productId')
    print productid
    if request.is_ajax():
        try:
            queryset= StoreProduct.objects.get_size(productid)
        except:
            queryset= None

        data = {
            "queryset" : queryset
        }

        return JsonResponse(data)

Here is my ajax script:

<script type="text/javascript">
    function getStoreView(event, productId) {
        event.preventDefault();   
        var data = {
           productId : productId
        }
        $.ajax({        
            type: "GET",
            url: "{% url 'storeView'  user=store.user %}",
            data: data,
            success: function(data) {
              console.log(data.queryset)
            },

            error: function(response, error) {
                alert(error);  
            }
        });
    };
</script>

What should I do to solve the problem above?
thanks in advance

+4
source share
2 answers

If you look at the error message from Django, you will see that it complains that the request is not serializable JSON. For ajax requests, you can see the answer using web browser development tools when DEBUG=True.

, , values(), , . -, .

queryset = StoreProduct.objects.get_size(productid)
values_list = list(queryset.values())
+6

json, json - . django :

from django.core import serializers

serialized_qs = serializers.serialize('json', queryset)
data = {"queryset" : serialized_qs}
return JsonResponse(data)

javascript data['queryset'] json.

+2

Source: https://habr.com/ru/post/1620775/


All Articles