Django - modal_forms:不显示错误

时间:2015-09-17 09:18:47

标签: django forms twitter-bootstrap

当我的表单无效时,不显示错误..为什么? :

我的表格:

#form for signup
class inscriptionForm(forms.Form):
    username = forms.CharField(label="Username",widget=forms.TextInput()
    mail = forms.EmailField(label="Email", widget=forms.EmailInput()
    mdp = forms.CharField(label="Password",widget=forms.PasswordInput())

    def clean_username(self):
        us = self.cleaned_data['username']
        test = User.objects.filter(username=us)
        if len(test)>0:
            raise forms.ValidationError("Username already exists.")
        return us

    def clean_mail(self):
        mail = self.cleaned_data['mail']
        testE = User.objects.filter(email=mail)
        if len(testE)>0:
            raise forms.ValidationError("Email already exists.")
        return mail

我的观点:

def inscription(request):
    error = False
    nextpage = None

    if request.method == "POST":
        form = inscriptionForm(request.POST)

        if request.method == "POST":
            if 'nextpage' in request.POST :
                nextpage = request.POST['nextpage']

        if form.is_valid():
            mail = form.cleaned_data["mail"]
            username = form.cleaned_data["username"]
            mdp = form.cleaned_data["mdp"]
            u = User.objects.create_user(username,mail,mdp)
            u.save()
            theuser = authenticate(username=pseudo, password=mdp)
            login(request,theuser)
            messages.add_message(request, messages.INFO, u'Welcome')
        else :
            error = True
    else :
        form = inscriptionForm()

    if nextpage != None :
        return redirect(nextpage)
    else : 
        return redirect(reverse(home), messages)

def home(request):
    formSignup = inscriptionForm(auto_id=False)
    ...
    return render(request,"the_site/home.html",locals())

最后我的模板,我使用模态引导程序(如弹出窗口):

 # ******* IN home.html : *********
    ...

    <form method="POST" action="{% url 'inscription' %}" class="">
        {% csrf_token %}

        <div class="row">
            <div class="col-md-offset-2 col-md-8">
                {% load bootstrap %}
                {{ formSignup|bootstrap }}
            </div>
        </div>
        <div class="div-button">
            <input type="submit" class="btn btn-info" value="Signup" style="text-align:center;">
        </div>

    </form>

    ....

因此,当我的表单提交且无效时,模板的模式形式中不会显示任何错误...

但是我在表单中写了clean_username和clean_mail ...

示例:如果用户编写用户名并且此用户名已存在,则表单不显示&#34;用户名已存在。&#34; ... 为什么?

我认为是由于&#34;返回重定向&#34;我的观点,但我不知道为什么以及如何解决它......

0 个答案:

没有答案