Re: For discussion: JSON-aware views for error responses

2022-04-23 Thread 'Adam Johnson' via 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".

On Sat, Apr 23, 2022 at 6:16 AM vanadium23  wrote:

> I've done some work long ago. May be will help for someone.
> https://github.com/django/django/pull/8947
>
> On Friday, 22 April 2022 at 08:53:56 UTC+3 Tobias Bengfort wrote:
>
>> +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
>>
> --
> 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-developers+unsubscr...@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
> 
> .
>

-- 
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-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAMyDDM0inQhY3q6oVZEV-ELmSYHKtHrHm53wJ2OQ68mq1JTpew%40mail.gmail.com.


Re: For discussion: JSON-aware views for error responses

2022-04-22 Thread vanadium23
I've done some work long ago. May be will help for someone.
https://github.com/django/django/pull/8947

On Friday, 22 April 2022 at 08:53:56 UTC+3 Tobias Bengfort wrote:

> +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
>

-- 
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-developers+unsubscr...@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.


Re: For discussion: JSON-aware views for error responses

2022-04-21 Thread Tobias Bengfort

+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

--
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-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/0cb96663-a937-e82f-8f50-43f279c14543%40posteo.de.


Re: For discussion: JSON-aware views for error responses

2022-04-21 Thread Jacob Rief
I encountered this problem many times myself, so I'm +1 for this proposal. 

– Jacob

-- 
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-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/639a72d1-ae5d-4bae-bd28-b3b5f1d2d955n%40googlegroups.com.


For discussion: JSON-aware views for error responses

2022-04-21 Thread Ville Säävuori
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

-- 
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-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/68ced6ec-342f-42f6-ba0b-1a51360fa119n%40googlegroups.com.