使用django身份验证系统

时间:2017-06-23 15:14:47

标签: python django python-2.7 login django-views

我想使用处理程序登录。

我的代码使用会话,但我想使用处理程序

我去过:

https://docs.djangoproject.com/en/1.11/topics/auth/default/

但我不明白完整。

我想记录用户(使用用户名或电子邮件和密码)

您是否拥有 stackoverflow github 或中的示例代码或项目代码。 。 。 ???

1 个答案:

答案 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,因此如果用户已登录一次,则不需要再次输入密码。