장고에서 Modal 창으로 처리하는 함수들을 위해 Django Bootstrap Modal Forms 라이브러리를 활용하고 있다. 홈페이지는 아래를 참고하세요.
https://github.com/trco/django-bootstrap-modal-forms
GitHub - trco/django-bootstrap-modal-forms: A Django plugin for creating AJAX driven forms in Bootstrap modal.
A Django plugin for creating AJAX driven forms in Bootstrap modal. - GitHub - trco/django-bootstrap-modal-forms: A Django plugin for creating AJAX driven forms in Bootstrap modal.
github.com
그런데 막상 사용해 보면, Form submit 이 2번씩 호출되는 것 같다. 확인해 보면 실제 2번씩 호출된다. DB쿼리 같으면 동일한 동작을 2번씩 수행하는 것과 같다.
이를 막고 싶으네. 당연히 이런 질문이 있었고,
https://stackoverflow.com/questions/60264166/bootstrap-modal-forms-posting-twice-django
Bootstrap Modal Forms Posting Twice Django
I am following the instructions from django-bootstrap-modal-forms and what I am finding is that my form is posting or submitting twice when I submit the form. First, the object was simply being cr...
stackoverflow.com
이를 막는 방법은 아래 예를 참고하자.
class AppCreateView(BSModalCreateView):
template_name = 'apps/app_create.html'
form_class = UserAppForm
success_message = 'Success: App was created.'
success_url = reverse_lazy('dashboard')
def form_valid(self, form):
if not self.request.is_ajax():
app = form.save(commit=False)
profile = Profile.objects.get(user=self.request.user)
app.profile = profile
app.save()
return HttpResponseRedirect(self.success_url)
핵심은 is_ajax() 함수 값을 보고 False 일때만 처리하도록 한다는 것. 즉 2번 호출 되는 것을 막지는 못했지만 2번 중복 동작을 막는것으로 일단 만족하자

끝.
Yuting Gao 님의 사진, 출처: Pexels