[Django]-Diagnosing AttributeError in Django view

4👍

As the error says, you’re returning a tuple: the JSON, an integer (presumably the status code) and a dict (presumably the headers).

Even if you fixed this, you can’t just return JSON from a view; you have to return an instance of HttpResponse or one of its subclasses.

Instead of doing return json.dumps(...) in your POST blocks, you should use JsonResponse. This accepts a Python datastructure and returns a response containing the serialized JSON; as a bonus, it also sets the content-type appropriately.

if event_type == 'project.datafile_updated':
    ...
    return JsonResponse({'success':True})
return JsonResponse({'success':False}, status=400)

(Note that render is explicitly a shortcut which renders a template and returns it as an HttpResponse; this isn’t the case with json.dumps().)

Leave a comment