0
我試圖在Django中創建自定義驗證標識符是電子郵件,有一個名爲和密碼字段所需的字段。登錄視圖工作正常,但我從表單中收到錯誤username
。用戶名錯誤在Django中使用自定義驗證
這裏是我的views.py
def auth_login(request):
if request.method == 'POST':
email = request.POST['email']
password = request.POST['password']
user = authenticate(email=email, password=password)
if user is not None:
login(request, user)
return HttpResponseRedirect("/tasks/")
else:
return HttpResponse('Invalid login.')
else:
form = UserCreationForm()
return render(request, "registration/login.html", {
'form': form,
})
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
new_user = form.save()
new_user = authenticate(email=request.POST['email'], password=request.POST['password1'])
login(request, new_user)
return HttpResponseRedirect("/tasks/")
else:
form = UserCreationForm()
return render(request, "registration/register.html", {
'form': form,
})
這裏是我的register.html
<form class="form-signin" role="form" method="post" action="">
{% csrf_token %}
<h2 class="form-signin-heading">Create an account</h2>
<input type="text" name="name" maxlength="30" class="form-control" placeholder="Username" required autofocus>
<br>
<input type="email" name="email" class="form-control" placeholder="Email" required>
<br>
<input type="password" name="password1" maxlength="4096" class="form-control" placeholder="Password" required>
<br>
<input type="password" name="password2" maxlength="4096" class="form-control" placeholder="Password confirmation" required>
<input type="hidden" name="next" value="/tasks/" />
<br>
<button class="btn btn-lg btn-primary btn-block" type="submit">Create the account</button>
</form>
{% if form.errors %}
{% for error in form.errors %}
{{ error }}
{% endfor %}
{% endif %}
此打印錯誤username
。這裏有什麼問題?
html表單中沒有'username'字段,這可能在'UserCreationForm'中需要。 – Rohan
如果是定製認證系統,註冊視圖如何工作? –