django 表单有效()失败

tho*_*ith 2 python forms django

我正在尝试使用 django 创建注册表单,当我提交表单时,is valid() 函数失败,我不知道为什么。我的注册和登录页面都在一个 html 页面上,尽管我将所有字段名称称为不同的名称,例如 login_username。

表格.py

class SignUpForm(forms.Form):
    username = forms.CharField(label='Username', max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter Username'}))
    conUsername = forms.CharField(label='Confirm Username', max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Confirm Username'}))
    firstName = forms.CharField(label='First Name', max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter Firstname'}))
    lastName = forms.CharField(label='Last Name', max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter LastName'}))
    email = forms.CharField(label='Email', max_length=220,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter Email','type':'email'}))
    conEmail = forms.CharField(label='Confirm Email', max_length=220,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Confirm Email','type':'email'}))
    password = forms.CharField(label="Confirm Password", max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','type':'password','placeholder':'Enter Password'}))
    conPassword = forms.CharField(label="Confirm Password", max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','type':'password','placeholder':'Confirm Password'}))
Run Code Online (Sandbox Code Playgroud)

视图.py

def SignUp(request):
    regForm = SignUpForm()
    form = LoginForm()
    if request.method == 'POST':

        if regForm.is_valid():
            username = regForm.cleaned_data('username')
            print username
            confirm_username = regForm.cleaned_data('conUsername')
            first_name = regForm.cleaned_data('firstName')
            last_name = regForm.cleaned_data('lastName')
            email = regForm.cleaned_data('email')
            confirm_email = regForm.cleaned_data('conEmail')
            password = regForm.cleaned_data('password')
            confirm_password = regForm.cleaned_data('conPassword')



                #try:
                    #user = User.objects.get(username=request.POST['username'])
                    #return render(request, 'index.html',{'error_message':'username must be unique'})
                #except User.DoesNotExist:  
                    #user = User.objects.create_user(request.POST['username'], password=request.POST['password'])
                    #login(request, user)
                    #return render(request, 'index.html')
        else: 
            return render(request,'index.html',{'username_error':'usernames didnt match','form':form,'regForm':regForm})


    else:
        return render(request, 'index.html',{'form':form,'regForm':regForm})
Run Code Online (Sandbox Code Playgroud)

索引.html

<form class="regForm" method="POST" action="{% url 'signup' %}">
        {% csrf_token %}
        {{username_error }}
        <div class="row">
        <div class="col-md-2 col-md-offset-8">
        <div class="form-group">
            {{regForm.error_message.username}}
           {{ regForm.username }}
        </div>
    </div>
      <div class="col-md-2 ">
    <div class="form-group">
      {{error_message.conUsername}}
           {{ regForm.conUsername }}
        </div>
    </div>
</div>
      <div class="row">
        <div class="col-md-2 col-md-offset-8">
        <div class="form-group">
           {{ regForm.firstName }}
        </div>
    </div>
        <div class="col-md-2">
    <div class="form-group">
           {{ regForm.lastName }}
        </div>
    </div>
</div>
        <div class="row">
        <div class="col-md-4 col-md-offset-8">
        <div class="form-group">
           {{ regForm.email }}
        </div>
    </div>
</div>
    <div class="row">
        <div class="col-md-4 col-md-offset-8">
        <div class="form-group">
           {{ regForm.conEmail }}
        </div>
    </div>
</div>
<div class="row">
        <div class="col-md-2 col-md-offset-8">
        <div class="form-group">
           {{ regForm.password }}
        </div>
    </div>
      <div class="col-md-2">
    <div class="form-group">
           {{ regForm.conPassword }}
        </div>
    </div>
</div>

      <div class="row">
        <div class="col-md-3 col-md-offset-9">
        <div class="form-group">
            <button type="submit" id="regBtn" class="btn btn-default">Submit</button>
        </div>
    </div>
</div>

      </form>
Run Code Online (Sandbox Code Playgroud)

dan*_*eia 7

现在您的表格始终为空,因此无效。您忘记将请求 POST 内容添加到表单中。

def SignUp(request):
    # ...
    if request.method == 'POST':
        regForm = SignupForm(request.POST)  # form needs content
        if regForm.is_valid():
            # ...
Run Code Online (Sandbox Code Playgroud)

如果您在拨打电话时遇到困难.is_valid(),您可以print(regForm.errors)查看发生了什么。