我想使用处理程序登录。
我的代码使用会话,但我想使用处理程序:
我去过:
https://docs.djangoproject.com/en/1.11/topics/auth/default/
但我不明白完整。
我想记录用户(使用用户名或电子邮件和密码)
您是否拥有 stackoverflow 或 github 或中的示例代码或项目代码。 。 。 ???
答案 0 :(得分:1)
如果您使用django.contrib.auth.models
from django.contrib.auth import authenticate, login
def user_login(request):
# check here that request.method is POST or not.
user = authenticate(username=request.POST.get('username'), password=request.POST.get('password'))
if user is not None:
login(request, user)
# send some http response here that login successful or redirect to some other page
else:
# return an error page saying that username password not correct
身份验证功能将检查数据库中用户表中的用户名和密码,如果它找到用户匹配的查询,则返回用户对象,否则返回None。您可能不想管理会话,因为django已经为每个成功登录的用户设置了cookie,因此如果用户已登录一次,则不需要再次输入密码。