For discussion: JSON-aware views for error responses

91 views
Skip to first unread message

Ville Säävuori

unread,
Apr 21, 2022, 3:45:44 PM4/21/22
to Django developers (Contributions to Django itself)
Hello Django devs!

I'm a long time Django user and nowadays work with sites where Django is mostly or only an API for the front end. I'm assuming this is not an exotic use case in 2022.

One pain point I continue to come across over and over again is that Django by default only speaks text/html even if the request has application/json accept headers. This often results in situatios where the unassuming JS app burps a HUGE html response as a string to the user when something goes wrong. And yes, it's obviously not Djangos fault if the frontend developer is sloppy but I believe there's lots of things we could improve here.

Some spesific things we could improve:
- At least for 400, 500, and CSRF errors, send JSON (or empty response) back instead of HTML if the request is xhr or has JSON headers
- Make it easy to override the JSON like we can do with HTML
- Add good documentation to explain how this works and how it can be customized

I'm interested in hearing comments about this, and opinions from the core devs if this would be something that could be built into Django. I haven't been active here for a looong time but this is a bit of a pet peeve of mine and I would be more than happy to implement this also.

And to be clear, I understand we already have middleware APIs and various settings to handle this but my point is that I think handling this in Django core (even as an optional setting or middleware) would be most useful and right way to do this for the community.

Sorry for the long post.

Cheers,

- VS

Jacob Rief

unread,
Apr 22, 2022, 1:36:49 AM4/22/22
to Django developers (Contributions to Django itself)
I encountered this problem many times myself, so I'm +1 for this proposal. 

– Jacob

Tobias Bengfort

unread,
Apr 22, 2022, 1:53:56 AM4/22/22
to django-d...@googlegroups.com
+1 from me.

On 14/04/2022 11.03, Ville Säävuori wrote:
> And to be clear, I understand we already have middleware APIs and
> various settings to handle this but my point is that I think handling
> this in Django core (even as an optional setting or middleware) would be
> most useful and right way to do this for the community.

Can you provide some examples? I support the general idea, but having
some concrete APIs to discuss might be helpful.

thanks
tobias

vanadium23

unread,
Apr 23, 2022, 1:16:48 AM4/23/22
to Django developers (Contributions to Django itself)
I've done some work long ago. May be will help for someone.
https://github.com/django/django/pull/8947

Adam Johnson

unread,
Apr 23, 2022, 5:22:12 AM4/23/22
to Django developers (Contributions to Django itself)
+1 from me

- At least for 400, 500, and CSRF errors, send JSON (or empty response) back instead of HTML if the request is xhr or has JSON headers

It's possible to detect requests for JSON with request.accepts() , which the previous PR from vanadium23 predates.

There's no way to detect "if the request is xhr" - we removed request.is_ajax because it only detected requests made with jQuery and some other libraries.
 
- Make it easy to override the JSON like we can do with HTML

I think we don't need to add new extension points. If you want custom JSON or HTML, you can provide your own views. These views can always "return custom JSON *or* call Django's built-in views".

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/744c3b96-4c42-4eac-a451-5c6f6da446fdn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages