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

Why my form is being submited 3 times? #44

Closed
bryannatali opened this issue Aug 8, 2019 · 6 comments
Closed

Why my form is being submited 3 times? #44

bryannatali opened this issue Aug 8, 2019 · 6 comments
Labels
Q&A Questions and answers

Comments

@bryannatali
Copy link

No description provided.

@bryannatali
Copy link
Author

bryannatali commented Aug 8, 2019

I think its happening 'cause the .each() jquery function isnt correct

My View Class

class KeyResultEditView(BSModalUpdateView):
	model = CompanyKeyResult
	template_name = 'modals/edit_keyresult.html'
	form_class = CompanyKeyResultForm
	success_message = 'Resultado chave alterado com sucesso.'
	success_url = reverse_lazy('okr_company')

	def dispatch(self, request, *args, **kwargs):
		self.key_result = get_object_or_404(CompanyKeyResult, pk=kwargs['pk'])
		ProgressHistory.objects.create(history_company_key_result=self.key_result, date=self.key_result.date, percent=self.key_result.percent)

		return super().dispatch(request, *args, **kwargs)

Url
path('modals/edit-kr/<int:pk>', views.KeyResultEditView.as_view(), name='edit_key_result'),

Js
$(".edit-kr").each(function(){ $(this).modalForm({formURL: $(this).data('id')}); });

@trco
Copy link
Owner

trco commented Aug 9, 2019

@bryannatali I would say that each function is not a problem. It's identical to the docs. What do you mean when saying that the form is submitted 3 times? It normal that it's submitted 2 times. Check #14.

@bryannatali
Copy link
Author

okay, thank you for quick response.

When I submit the modal this ProgressHistory.objects.create(history_company_key_result=self.key_result, date=self.key_result.date, percent=self.key_result.percent) happens three times. So the model is created three times on db.

@trco
Copy link
Owner

trco commented Aug 10, 2019

@bryannatali Ok, now I see. You defined a creation of another object inside of a dispatch method. Although it works and creates an object, my opinion is that you shouldn't use Django in this way. So you need to figure out how to move this code out of dispatch. When you POST the form dispatch is called. In case of my package two POSTs result in two dispatch calls, so this explains why two objects are created however I don't know where the third comes from. So reconsider your design and definitely don't abuse dispatch method.

@bryannatali
Copy link
Author

Oh, thank you again!

Based on what you said I changed my ViewClass to:

class KeyResultEditView(BSModalUpdateView):
    model = CompanyKeyResult
    template_name = 'modals/edit_keyresult.html'
    form_class = CompanyKeyResultForm
    success_message = 'Resultado chave alterado com sucesso.'
    success_url = reverse_lazy('okr_company')

    def dispatch(self, request, *args, **kwargs):
        self.key_result = get_object_or_404(CompanyKeyResult, pk=kwargs['pk'])
        return super().dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        try:
            ProgressHistory.objects.get(date=self.key_result.date, percent=self.key_result.percent)
        except ProgressHistory.DoesNotExist:
            ProgressHistory.objects.create(history_company_key_result=self.key_result, date=self.key_result.date, percent=self.key_result.percent)
        return super().form_valid(form)

and it works!

@trco
Copy link
Owner

trco commented Aug 12, 2019

@bryannatali That's great. I'm glad you made it. Star 70 highly appreciated ;)

@trco trco closed this as completed Aug 12, 2019
@trco trco added the Q&A Questions and answers label Aug 12, 2019
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

2 participants