django - 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)")
解决方案
发现我的错误。问题是我的 boostrap 错误地显示密码字段有错误,而实际上,复选框有错误,因为它没有默认值。
推荐阅读
- c++ - 没有得到以下代码的任何输出。任何人都可以看看这个
- python - 将语音训练为文本时不知道参数
- typescript - Vue3 Typescript VSCode Intellisense Props 扩展 Attrs
- docker - 从Jenkins主机的内部图像运行文件
- java - 如何有效地测试 JPA 存储库?
- database - Postgresql - 在同一会话中查找不同用户的重叠时间范围并将它们作为对呈现
- c# - 如何在选择 DataGridCell 时更改模板项的背景
- angular - 如何使用 (selectionChange) 方法更新基于另一个垫选择字段的输入字段
- jinja2 - Jinja2 未定义错误 jinja2.exceptions.UndefinedError:列表对象没有元素 0
- python - 我可以使用输入向 POSTGRES TABLE 添加内容吗?(Python)