#20270 closed Bug (fixed)
bug in ajax example
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Documentation | Version: | dev |
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 |
Description
https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-editing/#ajax-example
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 = form.save()
data = {
'pk': form.instance.pk,
}
return self.render_to_json_response(data)
else:
return super(AjaxableResponseMixin, self).form_valid(form)
Or somehow call the super form_valid and then return the HttpResponse.
Change History (6)
comment:1 by , 12 years ago
Cc: | added |
---|---|
Easy pickings: | set |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 12 years ago
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:4 by , 12 years ago
Has patch: | set |
---|
Here's a pull request based on my initial comment: https://github.com/django/django/pull/1026
comment:5 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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: