簡體   English   中英

django form.is_valid 返回 false

[英]django form.is_valid returns false

當我提交表單並檢查表單未提交並被重定向時。 驗證總是假的。 我輸入了所有文件,它仍然顯示相同。 為什么會一直這樣顯示? 有什么不對? 下面是我使用的視圖、模板和表單的文件。

視圖.py

def signincheck(request):

    if request.method == "POST":
        formsignin = FormSignup(request.POST,request.FILES)
        if formsignin.is_valid():
            return HttpResponse("Password not match 3")
            TbluserVar=Tbluser()
            TbluserVar.firstname=formsignin.cleaned_data['firstname']
            TbluserVar.lastname=formsignin.cleaned_data['lastname']
            TbluserVar.username=formsignin.cleaned_data['username']
            Password=formsignin.cleaned_data['password']
            ConfirmPassword=formsignin.cleaned_data['confirm_password']
            TbluserVar.mobilenumber=formsignin.cleaned_data['mobilenumber']
            Tbluser.dateofbirth=formsignin.cleaned_data['dob']
            Tbluser.dateofjoining=datetime.datetime.today()
            Tbluser.userlevel=0
            if Password != ConfirmPassword:
                messages.error(request,'Password and Confirm Password does not match')
                return redirect('/')

            else:
                try:
                    user = Tbluser.objects.get(username=formsignin.cleaned_data['username'])
                    messages.error(request,'Username not available. Try another one.')
                    return redirect('/')

                except:
                    PasswordEnc=hashlib.md5(Password.encode())
                    RealPassword=PasswordEnc.hexdigest()
                    TbluserVar.passwordenc=RealPassword
                    TbluserVar.save()
                    request.session['username']=TbluserVar.username
                    request.session['getFirstandLastName']=TbluserVar.firstname + " " + TbluserVar.lastname
                    FullName=request.session['getFirstandLastName']
                    return redirect('/index')

        else:
            return redirect('/')
    else:
        return HttpResponse("NOT CORRECT")

表格.py

class FormSignup(forms.Form):
    firstname=forms.CharField(
        max_length=30,
        widget=forms.TextInput(attrs={'placeholder': 'First Name...','class':'loginform'}))

    lastname=forms.CharField(
        max_length=30,
        widget=forms.TextInput(attrs={'placeholder': 'Last Name...','class':'loginform'}))

    username=forms.CharField(max_length=30,widget=forms.TextInput(attrs={'placeholder': 'User Name...','class':'loginform'}))
    password=forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'placeholder': 'Password...','class':'loginform'}))
    confirm_password=forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'placeholder': 'Confirm Password...','class':'loginform'}))
    mobilenumber=forms.CharField(max_length=30,widget=forms.TextInput(attrs={'placeholder': 'Mobile Number...','class':'loginform'}))
    dob=date = forms.DateTimeField(
        input_formats=['%d/%m/%Y'],
        widget=forms.TextInput(attrs=
        {
            'class':'datepicker',
            'placeholder': 'Date of Birth...'

        }))

模板.html

<div class="modal fade" id="modalSignUpForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header text-center">
                <h4 class="modal-title w-100 font-weight-bold">Sign in</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <form id="idlognform" method="POST" action="{% url 'signupcheck' %}" enctype="multipart/form-data">
                {% csrf_token %}
                <div class="modal-body mx-3">
                  <div class="md-form mb-4">
                    <i class="fas fa-lock prefix grey-text"></i>
                    {{FormSignup1.firstname}}
                  </div>

                  <div class="md-form mb-4">
                    <i class="fas fa-lock prefix grey-text"></i>
                    {{FormSignup1.lastname}}
                  </div>

                  <div class="md-form mb-4">
                    <i class="fas fa-lock prefix grey-text"></i>
                    {{FormSignup1.username}}
                  </div>

                  <div class="md-form mb-4">
                    <i class="fas fa-lock prefix grey-text"></i>
                    {{FormSignup1.password}}
                  </div>

                  <div class="md-form mb-4">
                    <i class="fas fa-lock prefix grey-text"></i>
                    {{FormSignup1.confirm_password}}
                  </div>

                  <div class="md-form mb-4">
                    <i class="fas fa-lock prefix grey-text"></i>
                    {{FormSignup1.mobilenumber}}
                  </div>

                  <div class="md-form mb-4">
                    <i class="fas fa-lock prefix grey-text"></i>
                    {{FormSignup1.dob}}
                  </div>
                  <div class="modal-footer d-flex justify-content-center">
                      <input class="btn btn-default" type="submit" id="signinbutton" value="Login">
                    </div>
                </div>
                {{form.errors}}
              </form>


            </div>
          </div>
        </div>

這是因為在 forms.py 中您使用的是dob=date= 它看起來不像一個有效的 Django 表單。 希望這有幫助。

我稍后會嘗試您的代碼。 無論如何只是閱讀你可以試試這個:在views.py中放入if formsignin.is_valid()的else:

formsignin = FormSignup()

但也許上述行為不是你想要的。 尤其是在 form.py 中:

def __init__(self, *args, **kwargs):
    super(FormSignup, self).__init__(*args, **kwargs)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM