django-bootstrap-modal-forms
django-bootstrap-modal-forms copied to clipboard
Double post when submitting modal form
Hi,
I have a BSModalFormView with a BSModalForm for the form.
When I submit the form it double posts.
Since I'm deleting the initial result, when it calls the post the second time returns 404.
After a bit of research I've found Stack overflow post which seems to be the same.
The top answer says the package was updated and that fixes the problem, however it is still doing it for me.
When I've tried to print the self.request.is_ajax() I get the 'WSGIRequest' object has no attribute 'is_ajax' which brought me to this call was depreciated in Django 3.1.
Which from the depreciation notes lead me to:
def form_valid(self, form: Any) -> HttpResponse:
valid = super().form_valid(form)
if not valid:
return valid
if self.request.headers.get('x-requested-with'): # Ajax check. Will skip on first call
return valid
# rest of validation stuff here.
# delete initial model here.
return valid
The check for the header will not save the form on the first call only on the second.
This doesn't seem a very pythonic answer to me.
This bug ruined my whole week! I fixed it, you can use this file in my repo: https://github.com/amir4v/dev/blob/main/jquery-django-bootstrap-modal-forms.js
I confirm this issue is not resolved from last released package version (3.0.4).
Also the workaround from @amir4v with patched Javascript is not enough (maybe there is some view patch to apply also?), it changes some behavior but it does not fix double POST request.
Hey guys! It would be optimal to have working=non-working example, this way I could check this out faster, maybe even during next weekend. Please check https://github.com/trco/django-bootstrap-modal-forms#opening-an-issue.
My first impression is that we should add an option where developer would be able to define if he wants to skip ajax form validation when using BSModalForm and maybe even BSModalModelForm. I believe you hit the case I never thought about.
Of course the override of some predefined methods could also be the answer.
I've solved the issue (at least for now) with this is_ajax replacement based on https://stackoverflow.com/a/60856647/3642184
Using: Django 3.2.23 (LTS) Djano Bootstrap Modal Forms 3.0.4
class TestimonialFormView(BSModalFormView):
...
def form_valid(self, form):
...
if not self.request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest':
DO_STUFF_ONLY_ONCE_HERE
response = super().form_valid(form)
return response
I've solved the issue (at least for now) with this
is_ajaxreplacement based on https://stackoverflow.com/a/60856647/3642184Using: Django 3.2.23 (LTS) Djano Bootstrap Modal Forms 3.0.4
class TestimonialFormView(BSModalFormView): ... def form_valid(self, form): ... if not self.request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest': DO_STUFF_ONLY_ONCE_HERE response = super().form_valid(form) return response
Can confirm. Implemented in 08/2022 the same solution and added a comment for future me on why I did this. django-bootstrap-modal even provides an own implementation of is_ajax (which basically is @fergbrain 's solution), which can be used (https://github.com/trco/django-bootstrap-modal-forms/blob/master/bootstrap_modal_forms/mixins.py#L104-L105).
from bootstrap_modal_forms.mixins import is_ajax
...
if request.method == "POST":
if self.is_valid():
if not is_ajax(request.META):
# Modal forms send one POST for checking on data validity. This can be used to return possible errors
# on the form. A second POST (if no errors occured) is sent afterwards and needs to process the
# saving/commiting of the data to the database. is_ajax() performs this check. The first request is
# an ajax call, the second is a regular form POST.
self.save()
messages.success(
request,
msg_success
)
return HttpResponseRedirect(redirect_url)
else:
context = {
"form": self,
}
context = BaseContext(request, context).context
return render(request, template, context)