Z
Z
zlodiak2019-06-24 15:32:51
Django
zlodiak, 2019-06-24 15:32:51

How to add headers for json response?

Please help me to add http header to JSON format response:

def result(request, date_from, date_to, user_id):
    results = Result.objects.filter(
        user_id=user_id, 
        created_date__gte=date_from[0:10], 
        created_date__lte=date_to[0:10],
    )   
    results_serialized = serializers.serialize('json', results)
    return JsonResponse(results_serialized, safe=False)

I know how to add a title if the view returns some kind of template, but this is a different case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-06-24
@zlodiak

response = JsonResponse(results_serialized, safe=False)
response['some-header'] = 42
return response

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question