【问题标题】:Django - "This Field is Required" error on Password fieldDjango - 密码字段上的“此字段是必需的”错误
【发布时间】:2021-04-10 00:09:20
【问题描述】:

在我的 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)")

【问题讨论】:

  • 请附上SignUpForm类。

标签: django forms authentication django-users


【解决方案1】:

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

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

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

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

【讨论】:

  • 我认为我的 signup.html 中的 field.errors 已经处理了每个人的字段 + 错误?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2016-04-29
    • 2020-06-02
    • 2019-04-13
    • 2012-12-02
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多