Opened 12 months ago

Closed 12 months ago

Last modified 12 months ago

#20270 closed Bug (fixed)

bug in ajax example

Reported by: tejinderss@… Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: bmispelon@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


This example does not work with CreateView or UpdateView because the form_valid overriding never saves the form and neither does it call its super class which does. So in order for CreateView to create new object, we would either have to add:

def form_valid(self, form):

if self.request.is_ajax():

self.object =
data = {


return self.render_to_json_response(data)


return super(AjaxableResponseMixin, self).form_valid(form)

Or somehow call the super form_valid and then return the HttpResponse.

Attachments (0)

Change History (6)

comment:1 Changed 12 months ago by bmispelon

  • Cc bmispelon@… added
  • Easy pickings set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

I like the second option better, because it would make this mixin more composable with other ones (in general, I find that CBV mixins should always call the super(...) implementation of the method they extend).

I'm not too sure how to achieve this elegantly though. Maybe something like that:

def form_valid(self, form):
    response = super(AjaxableResponseMixin, self).form_valid(form)
    if self.request.is_ajax():
        json_context = {'pk':}
        return self.render_to_json(json_context)
    return response

comment:2 Changed 12 months ago by anonymous

With the second approach there must be defined "success_url" in the subclass otherwise it errors out. But with json response view i don't think success_url should be a required parameter. Or we can set bogus success_url in this mixin if the request type is ajax?

comment:3 Changed 12 months ago by anonymous

Nevermind we do need success_url for non Ajax response anyway.

comment:4 Changed 12 months ago by bmispelon

  • Has patch set

Here's a pull request based on my initial comment:

comment:5 Changed 12 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 73c26f0c95e83582e933e0ef2f71a013f7a9740e:

Fixed #20270 -- Fixed error in AjaxResponseMixin documentation

comment:6 Changed 12 months ago by Claude Paroz <claude@…>

In 6bb8df0a982bb394f2cf33cfc9d9d71589515801:

[1.5.x] Fixed #20270 -- Fixed error in AjaxResponseMixin documentation

Backport of 73c26f0c95 from master.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.