我想在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>
有什麼不對嗎?
這沒有什麼區別。它仍然重定向到註冊頁面。 –
在註冊方法中放置了一個用於表單驗證的else語句。這真的有效嗎? –
你說得對,表格無效。任何線索爲什麼? –