Django - 密码字段上的“此字段是必需的”错误

问题描述

在我的 Django 网站上,我无法提交表单。尽管我填写了每个字段,但我的密码字段仍然显示“此字段是必需的”,即使我也在密码字段中输入了一些内容。

我尝试删除密码标签的必需属性,但没有帮助。

这是我的代码。:

F

views.py:专门用于函数注册,代码永远不会到达 if form.is_valid() 块内。

def signup(request):

    #If user completes the form (hits the sign up button)
    #Send form data to url /signup-complete/ (see signup.html)
    if request.method == 'POST':

        form = SignUpForm(request.POST)

        if form.is_valid():

            user = User.objects.create(

                username= form.cleaned_data['email'],#Note although there is an email field,we don't use it to prevent redundancy
                first_name = form.cleaned_data['first_name'],last_name = form.cleaned_data['last_name'],password = form.cleaned_data['password'],)

            #Extended attribute,must be added seperately
            user.user_extend.is_student = form.cleaned_data['is_student']
            user.save()

            print('user is now created')

            return HttpResponseRedirect('')

    #Render form
    else:

        form = SignUpForm()

    return render(request,'mainapp/signup.html',{'form': form})

signup.html

<!-- action="/signup_complete/"-->
    <form action="" method="post">
        <!--Security,Cross Site Request Forgery Protection -->
        {% csrf_token %}
        {{ form.non_field_errors }}

        {% for field in form %}
        <div class="form-group">

            {{ field.errors }}
            <!--or field.name == 'confirm_password'-->
            {% if field.name == 'password' %}
            {{ field.label_tag }}
            <input name="{{ field.name }}" class="form-control" type="password" required>

            {% elif field.name == 'email'%}
            {{ field.label_tag }}
            <input name="{{ field.name }}" class="form-control" type="email" required>

            {% elif field.name == 'is_student'%}
            <div class="float-right">
                <input name="{{ field.name }}" type="checkbox">
                {{ field.label_tag }}

            </div>

            <!--First and last name -->
            {% else %}
            {{ field.label_tag }}
            <input name="{{ field.name }}" class="form-control" type="text" required>
            {% endif %}
        </div>


        {% endfor %}

        <!-- Sign up button -->
        <button type="submit" class="btn btn-primary col-md-12" value="sign_up">Sign Up!</button>

        </form>

models.py

class UserExtend(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    is_student = models.BooleanField(default=None)

SignUpForm (forms.py)

class SignUpForm(forms.Form):
    first_name = forms.CharField(label='First Name',max_length=50)
    last_name = forms.CharField(label='Last Name',max_length=50)
    email = forms.EmailField()
    password = forms.CharField(widget=forms.PasswordInput,max_length=50)
    
    is_student = forms.BooleanField(label="Are you a student? (Statistical purposes only)")

解决方法

首先,我建议您仔细查看请求,以找到您通过的字段。

您可以更简单地显示表单。

<form>
{{form.password.errors}}

<input name="password" class="classes">
</form>
,

发现我的错误。问题是我的 boostrap 错误地显示密码字段有错误,而实际上,复选框有错误,因为它没有默认值。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...