Giter Club home page Giter Club logo

Comments (5)

fergbrain avatar fergbrain commented on August 11, 2024 1

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

from django-bootstrap-modal-forms.

amir4v avatar amir4v commented on August 11, 2024

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

from django-bootstrap-modal-forms.

sveetch avatar sveetch commented on August 11, 2024

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.

from django-bootstrap-modal-forms.

trco avatar trco commented on August 11, 2024

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.

from django-bootstrap-modal-forms.

Rastopapola avatar Rastopapola commented on August 11, 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)

from django-bootstrap-modal-forms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.