首页 > 解决方案 > 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})

注册.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>

模型.py

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

注册表格 (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)")

标签: djangoformsauthenticationdjango-users

解决方案


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


推荐阅读