【发布时间】:2021-10-18 03:43:38
【问题描述】:
我无法在模板上呈现表单用户名。就像我想自动呈现我在forms.py 文件中定义的表单字段,但它不起作用。在这里,您可以在下面查看我的详细文件
forms.py
class CreateUserForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
views.py
from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
我的看法
从 .forms 导入 CreateUserForm
注册
def regPage(请求): form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
context = {'form':form}
return render(request, 'web/car-finder-home.html')
html
<form class="needs-validation" method="POST" action="">
{% csrf_token %}
<div class="mb-4">
<label class="form-label text-light" for="signup-name">Username</label>
{{ form.username }}
</div>
<div class="mb-4">
<label class="form-label text-light" for="signup-email">Email address</label>
<input class="form-control form-control-light" type="email" id="signup-email" placeholder="Enter your email" required />
</div>
<div class="mb-4">
<label class="form-label text-light" for="signup-password">Password <span class="fs-sm opacity-50">min. 8 char</span></label>
<div class="password-toggle">
<input class="form-control form-control-light" type="password" id="signup-password" minlength="8" required />
<label class="password-toggle-btn" aria-label="Show/hide password"> <input class="password-toggle-check" type="checkbox" /><span class="password-toggle-indicator"></span> </label>
</div>
</div>
<div class="mb-4">
<label class="form-label text-light" for="signup-password-confirm">Confirm password</label>
<div class="password-toggle">
<input class="form-control form-control-light" type="password" id="signup-password-confirm" minlength="8" required />
<label class="password-toggle-btn" aria-label="Show/hide password"> <input class="password-toggle-check" type="checkbox" /><span class="password-toggle-indicator"></span> </label>
</div>
</div>
<div class="form-check form-check-light mb-4">
<input class="form-check-input" type="checkbox" id="agree-to-terms" required />
<label class="form-check-label" for="agree-to-terms">
<span class="opacity-70">By joining, I agree to the</span> <a href="#" class="text-light">Terms of use</a> <span class="opacity-70">and</span> <a href="#" class="text-light">Privacy policy</a>
</label>
</div>
<button class="btn btn-primary btn-lg w-100" type="submit">Sign up</button>
</form>
【问题讨论】: