I have a form in modal bootstrap (pop up). The problem is that when the form is not cleaned by Django, the errors are written in red below the form fields but the modal closes.
The user think the form is validate but no.
My form :
class signupForm(forms.Form):
username = forms.CharField(max_length=25)
mail = forms.EmailField(label="Email", max_length=82)
password = forms.CharField(max_length=50)
def clean_mail(self):
mail = self.cleaned_data['mail']
testMail = User.objects.filter(email=mail)
if len(testMail)>0:
raise forms.ValidationError("Already used.")
return mail
The view :
def signup(request):
if request.method == "POST":
form = signup(request.POST)
if form.is_valid():
mail = form.cleaned_data["mail"]
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
u = User.objects.create_user(username,mail,password)
u.save()
the_user = authenticate(username=username, password=password)
login(request,the_user)
return redirect(...)
else :
error = True
else :
form = signupForm()
return render(request,...)
Finally the most important, the template :
<a href="#" data-toggle="modal" data-target="#myModal"><i> Signup </i></a>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title"> <b> Signup </b> </h3>
</div>
<div class="modal-body">
{% load bootstrap %}
<form method="POST" action="{% url 'signup' %}" class="">
{% csrf_token %}
<div class="row">
<div class="col-md-offset-2 col-md-8">
{{ formSignup|bootstrap }}
</div>
</div>
<div class="div-button">
<input type="submit" class="btn btn-info" value="Sign up">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Quit</button>
</div>
</div>
</div>
</div>
How to keep the signup form (modal bootstrap) when the form is not cleaned ?