Skip to main content
 首页 » 编程设计

python之django 表单有效()失败

2025年05月04日39findumars

我正在尝试使用 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'})) 

View .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}) 

index.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> 

请您参考如下方法:

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

def SignUp(request): 
    # ... 
    if request.method == 'POST': 
        regForm = SignupForm(request.POST)  # form needs content 
        if regForm.is_valid(): 
            # ... 

如果您在调用 .is_valid() 时卡住了,您可以print(regForm.errors) 看看发生了什么。