1
我試圖讓自己的自定義登錄頁面來測試我的django技能,但它永遠不會工作。我的觀點:我的自定義django登錄視圖將不起作用
def login_view(request):
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data.get('email')
password = form.cleaned_data.get('password')
user = authenticate(email=email, password=password)
if user:
login(request, user)
redirect('/account/')
else:
print(str(password)+" "+str(email))
else:
form = LoginForm()
return render(request, 'users/login.html',{'form': form})
我的形式
class LoginForm(forms.Form):
email = forms.CharField(
widget=forms.TextInput(attrs={'class': 'registerforms', 'placeholder': 'Email'}),
label='',
)
password = forms.CharField(
widget=forms.PasswordInput(attrs={'class': 'registerforms', 'placeholder': 'Password'}),
label='',
)
我的網址
url(r'^login/$', views.login_view, name='login_view'),
我的模板
<form action="" method="post" role="form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
你可以發佈堆棧跟蹤,如果有的話? 您確定每次嘗試登錄時表單都有效嗎? – zaidfazil