Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double post when submitting modal form #230

Open
jeffm89 opened this issue Aug 15, 2023 · 5 comments
Open

Double post when submitting modal form #230

jeffm89 opened this issue Aug 15, 2023 · 5 comments
Assignees
Labels
Q&A Questions and answers

Comments

@jeffm89
Copy link
Contributor

jeffm89 commented Aug 15, 2023

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.

@trco trco self-assigned this Sep 2, 2023
@trco trco added the Q&A Questions and answers label Sep 2, 2023
@amir4v
Copy link

amir4v commented Oct 24, 2023

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

@sveetch
Copy link

sveetch commented Nov 13, 2023

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.

@trco
Copy link
Owner

trco commented Nov 13, 2023

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.

@fergbrain
Copy link

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

@Rastopapola
Copy link
Contributor

Rastopapola commented Jun 14, 2024

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

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Q&A Questions and answers
Projects
None yet
Development

No branches or pull requests

6 participants