Skip to content

Commit

Permalink
Fix form validation success url (#221)
Browse files Browse the repository at this point in the history
* Fix redirect of FormValidationMixin to be compliant to standard django:
If return class property success_url if exists, if not return value from classes get_success_url method. If that doesn't exist return value from models get_absolute_url method.

* Fix success url to work with all constellations
  • Loading branch information
christianwgd authored May 2, 2023
1 parent 640c614 commit af1bda1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions bootstrap_modal_forms/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ class FormValidationMixin:
"""
Generic View Mixin which saves object and redirects to success_url if request is not ajax request. Otherwise response 204 No content is returned.
"""


def get_success_url(self):
if self.success_url:
return self.success_url
return super().get_success_url()

def form_valid(self, form):
isAjaxRequest = is_ajax(self.request.META)
asyncUpdate = self.request.POST.get('asyncUpdate') == 'True'
Expand All @@ -86,7 +91,7 @@ def form_valid(self, form):
if asyncUpdate:
form.save()
return HttpResponse(status=204)

form.save()
messages.success(self.request, self.success_message)
return HttpResponseRedirect(self.get_success_url())
Expand Down

0 comments on commit af1bda1

Please sign in to comment.