一:Auth组件
-django内置的用户认证系统,可以快速的实现,登录,注销,修改密码...
-怎么用?
(1)先创建超级用户:
-python3 manage.py createsuperuser
-输入用户名,邮箱,密码,敲回车,这样就创建出一个超级用户
-也就是在auth_user这个表中插入了一条数据(密码是加密的,所以不能动手插入)
         (2)验证用户:
-from django.contrib import auth
-user = auth.authenticate(request,username = name,password= pwd)
-相当于在查询:uesr = models.User.objects.filter(name=name,pwd=pwd).first()
	     -如果校验通过,会返回一个user对象,通过判断user对象,校验是否验证成功
# 在views.py中
from django.shortcuts import render, HttpResponse

# Create your views here.
from django.contrib import auth


def login(request):
    if request.method == 'GET':

        return render(request, 'login.html')
    elif request.method == 'POST':
        name = request.POST.get('name')
        pwd = request.POST.get('pwd')
        user = auth.authenticate(request, username=name, password=pwd)

        if user:
            auth.login(request, user)
            return HttpResponse('登陆成功')
        else:
            return HttpResponse('用户名或密码错误')




# 在login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="post">
    {% csrf_token %}
    <p>用户名: <input type="text" name="name"></p>
    <p>密码:<input type="text" name="pwd"></p>
    <p><input type="submit" value="提交"></p>
</form>
</body>
</html>
用户的注册验证

相关文章:

  • 2022-01-08
  • 2021-10-29
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
  • 2021-12-24
猜你喜欢
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
  • 2022-02-25
相关资源
相似解决方案