[Django] #22591: Changing of HTTP status code in form_invalid()

18 views
Skip to first unread message

Django

unread,
May 6, 2014, 11:33:07 PM5/6/14
to django-...@googlegroups.com
#22591: Changing of HTTP status code in form_invalid()
-----------------------------+--------------------
Reporter: kevmo314@… | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Keywords: forms
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------
Would it be possible to permit the changing of status codes on
form_invalid? We had a form where we wanted to return a 400 status code
when a validation error occurs, but by default Django spits out a 200
error code. In other words, following the example at
[https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-
editing/#ajax-example], we'd like to return a 400 on a non-ajax request as
well.

Here's the change I'd like to propose. On line 78 of
django/views/generic/edit.py:

{{{
def form_invalid(self, form, **kwargs):
"""
If the form is invalid, re-render the context data with the
data-filled form and errors.
"""
return self.render_to_response(self.get_context_data(form=form),
**kwargs)
}}}

This would permit doing something like this:

{{{
response = super(AjaxableResponseMixin, self).form_invalid(form,
status=400)
}}}

Alternatively, a failure status code property may be more intuitive to
developers, but I think the above permits a bit more flexibility in terms
of modifying other attributes of the response as well.

--
Ticket URL: <https://code.djangoproject.com/ticket/22591>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 7, 2014, 7:48:52 AM5/7/14
to django-...@googlegroups.com
#22591: Changing of HTTP status code in form_invalid()
-----------------------------+--------------------------------------

Reporter: kevmo314@… | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:

Keywords: forms | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

The main issue I see with this is that you're hijacking `kwargs` for
`render_to_response()`. It seems equally likely that someone could propose
passing them to `get_context_data()`. To achieve what you're after, I
wonder if it would be better to override `render_to_response()` with
something like:
{{{
form = context.get('form')
if form and not form.is_valid():
response_kwargs['status'] = 400
super().render_to_response(context, **response_kwargs)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22591#comment:1>

Django

unread,
May 12, 2014, 8:40:28 AM5/12/14
to django-...@googlegroups.com
#22591: Changing of HTTP status code in form_invalid()
-----------------------------+--------------------------------------
Reporter: kevmo314@… | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: wontfix

Keywords: forms | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by timo):

* status: new => closed
* resolution: => wontfix


--
Ticket URL: <https://code.djangoproject.com/ticket/22591#comment:2>

Django

unread,
Nov 19, 2019, 8:15:23 AM11/19/19
to django-...@googlegroups.com
#22591: Changing of HTTP status code in form_invalid()
-----------------------------+--------------------------------------
Reporter: kevmo314@… | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: wontfix

Keywords: forms | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by Thomas Güttler):

http status code "200" on POST and invalid data is a bit strange. Django
does this since ages, nevertheless I think there should at least be simple
way to specify a different http status code.

Maybe give [https://docs.djangoproject.com/en/2.2/ref/class-based-views
/mixins-editing/#formmixin FormMixin] an overwritable attribute
http_status_on_invalid. This
should default to the old "200", but would allow subclasses to overwrite
it easily without coding.

--
Ticket URL: <https://code.djangoproject.com/ticket/22591#comment:3>

Django

unread,
Apr 14, 2021, 2:56:25 PM4/14/21
to django-...@googlegroups.com
#22591: Changing of HTTP status code in form_invalid()
-----------------------------+--------------------------------------
Reporter: kevmo314@… | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: wontfix

Keywords: forms | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by Adam Wróbel):

* cc: Adam Wróbel (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/22591#comment:4>

Reply all
Reply to author
Forward
0 new messages