python - django login get AuthenticationForm 不包含 get
问题描述
我正在尝试构建一个简单的登录表单,但我总是得到'AuthenticationForm' object has no attribute 'get'
。我是使用 django 框架的新手,但我不确定如何解决问题......任何提示而不改变太多逻辑?
view.py
from django.shortcuts import render, redirect
from django.views.generic import TemplateView
from django.contrib.auth import authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponse
class loginPage(TemplateView):
template_name = 'login.html'
def post(self, request):
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
user = authenticate(
request,
username=form.get('email'),
password=form.get('password')
)
if user is not None:
return HttpResponse("Testing")
else:
print(user)
else:
return HttpResponse("loginPage") ## always goes here
登录html
<form method="post">
{% csrf_token %}
<input class="form-control" name="email" placeholder="Email" /></div>
<input class="form-control" type="password" name="password" placeholder="Password" />
</form>```
解决方案
if form.is_valid():
username=form.cleaned_data['email']
password=form.cleaned_data['password']
user = authenticate(
request,
username=username,
password=password
)
如果这个工作,试试这个
推荐阅读
- angular - PrimeNg:ViewChildren QueryList 始终为空
- fiware - Orion CB:无法通过 /v2/entities 端点访问实体
- azure - 有没有办法找到有错误的文件?
- php - 名为“”的 Doctrine ORM 管理器不存在 - 语法错误
- node.js - 在 oauth20 中集成 json Web 令牌
- wordpress - 如何在 WordPress 的 gtranslate 插件中使用浏览器语言或用户 IP 地址翻译自动站点?
- excel - 我不好:VBA 混乱
- javascript - Javascript字符串匹配特定的正则表达式
- dsl - 如何弃用生成的 Xtext 元素?
- laravel - 在 Laravel 5.7 中尝试登录时显示验证您的电子邮件地址