gpt4 book ai didi

python - 名字和姓氏未以 Django 形式显示

转载 作者:行者123 更新时间:2023-11-28 21:39:02 25 4
gpt4 key购买 nike

我正在尝试创建包含以下信息(名字、姓氏、电子邮件、密码、password_confirm)的注册表单,但我没有得到名字和姓氏。到目前为止,我只得到用户名、密码和密码确认。为了添加缺失的字段,我做错了什么?

表单.py

class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=False, label='Fornavn', help_text='Optional.')
last_name = forms.CharField(max_length=30, required=False, label='Efternavn', help_text='Optional.')
email = forms.EmailField(max_length=254, label='Email', help_text='Required. Inform a valid email address.')

class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email')

register.html

{% for field in form %}
<div class="form-group">
<label>{{ field.label }}</label>
{% render_field field class="form-control" placeholder=field.help_text %}
</div>
{% endfor %}

views.py

def register(request):
if request.user.is_authenticated():
return redirect('/account/')
context = {}
if request.method == "POST":
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('/account/')
else:
form = UserCreationForm()
return render(request, 'register.html', {'form': form})

网址

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^register/$', views.register, name='register')

]

最佳答案

您正在为 GET(和其他)请求传递 UserCreationForm 而不是 SignUpForm:

if request.method == "POST":
...
else:
form = SignUpForm()

关于python - 名字和姓氏未以 Django 形式显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47334964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com