django-bootstrap-modal-forms
django-bootstrap-modal-forms copied to clipboard
Success message always duplicated
Hi, thanks again for the great project. Meanwhile, I faced with an issue, BSModalCreateView success_message always duplicates on form saving.
I have used the same view on my previous project based on Django 3.2.2 and it has no same behavior.
Current setup Django 3.2.4 django-bootstrap-modal-forms 2.2.0
class UserCreateView(BSModalCreateView):
template_name = 'includes/_modal_form.html'
form_class = UserCreationForm
success_message = 'Success: User was created.'
success_url = reverse_lazy('users')
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['title'] = "title text"
return context
Form
class UserCreationForm(BSModalModelForm):
password1 = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput(
attrs={'autocomplete': 'new-password', 'class': 'form-control'}),
help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput(
attrs={'autocomplete': 'new-password', 'class': 'form-control'}),
strip=False,
help_text=_("Enter the same password as before, for verification."),
)
class Meta:
model = User
fields = ['email', 'name', ]
error_css_class = 'is-invalid'
Button
<button class="modal_button bs-modal btn btn-primary btn-round ml-auto" type="button" name="button" data-form-url="{% url 'users_create' %}">
<i class="fa fa-plus"></i>Add Row
</button>
JS
setTimeout(
function()
{
$(".modal_button").each(function () {
$(this).modalForm({
formURL: $(this).data('form-url'),
errorClass: ".is-invalid",
modalID: "#modal",
});
});
}, 2000);
I'm also tried to do it this way, in this case, the message was not duplicated.
def form_valid(self, form):
if not self.request.is_ajax():
messages.success(self.request, 'Form submission successful')
return super().form_valid(form)
Looks like BSModalCreateView calls SuccessMessageMixin twice and it is a problem. If @trco confirms this, I can create PR with a new SuccessMessageMixin for the django-bootstrap-modal-forms with uitls.is_ajax() check.
I'm sorry, it looks like not a django-bootstrap-modal-forms issue.
Messages get duplicates only in cases when BSModalCreateView success_url point to the DatatableView (from django-datatable-view)
Not sure which module is the culprit, but if I extend Django's SuccessMessageMixin with is_ajax condition check - everything works fine. But I not sure how to safe use own SuccessMessageMixin with the django-bootstrap-modal-forms without creating custom classes for all django-bootstrap-modal-forms views because I'll will lost updates able.
Solved with latest 3.0.0 release.