标签:tor action create close family round charset 开发 port
一:Auth模块简介
二:Auth模块常用方法
三:扩展默认的auth_user表
一:Auth模块简介
Auth模块是Django自带的用户认证模块:
我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统。此时我们需要实现包括用户注册、用户登录、用户认证、注销、修改密码等功能
Django内置了强大的用户认证系统--auth,它默认使用 auth_user 表来存储用户数据。
链接数据库后有自带的auth_user表
二:Auth模块常用方法
from django.contrib import auth from django.contrib.auth.decorators import login_required def auth_test(request): if request.method==‘POST‘: name=request.POST.get(‘name‘) pwd=request.POST.get(‘pwd‘) #做认证 user=auth.authenticate(request,username=name,password=pwd) # user=models.User.objece.filter(username=name,password=pwd).first() if user: # 做登录 auth.login(request,user) return HttpResponse(‘登录成功‘) else: return HttpResponse(‘登录失败‘) return render(request,‘index.html‘) def auth_test2(request): # print(request.user.username) # print(request.user.password) # print(type(request.user)) print(request.user) return render(request,‘auth_test.html‘) def auth_logout(request): auth.logout(request) return HttpResponse(‘注销成功‘) @login_required def auth_createuser(request): # UserInfo.objects.create() # user1=UserInfo.objects.create_superuser(username=‘lqz1‘,password=‘lqz12345‘,email=‘88@qq.com‘) # user2=UserInfo.objects.create_user(username=‘lqz2‘,password=‘lqz12345‘,email=‘88@qq.com‘) # user = auth.authenticate(request, username=‘lqz1‘, password=‘lqz12345‘) # if user: # print(‘1111‘) # else: # print(‘222‘) if request.user.is_authenticated(): print(‘登录了‘,request.user) else: print(‘没有登录‘,request.user) # user=request.user # 校验密码 # ret=user.check_password(‘lqz12345‘) # print(ret) # 设置密码 # print(user) # !!!!!!一定要save # user.set_password(‘lqz11111‘) # user.save() return HttpResponse(‘注册成功‘)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% if request.user.is_authenticated %} {% endif %} </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="/static/jquery-3.3.1.js"></script> <title>Title</title> </head> <body> <form action="" method="post"> {% csrf_token %} <p>用户名:<input type="text" name="name" id="name"></p> <p>密码:<input type="password" name="pwd"></p> <input type="submit"> </form> <button id="ajax">ajax</button> </body> <script> $("#ajax").click(function () { $.ajax({ url:‘‘, type:‘post‘, //data:{‘name‘:$("#name").val(),‘csrfmiddlewaretoken‘:$(‘[name="csrfmiddlewaretoken"]‘).val()}, data:{‘name‘:$("#name").val(),‘csrfmiddlewaretoken‘:‘{{ csrf_token }}‘}, success:function (data) { console.log(data) } }) }) </script> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="/static/jquery-3.3.1.js"></script> <title>Title</title> </head> <body> <form action="" method="post"> {# {% csrf_token %}#} <p>用户名:<input type="text" name="name" id="name"></p> <input type="submit"> </form> <button id="ajax">ajax</button> </body> <script> $("#ajax").click(function () { $.ajax({ url:‘‘, type:‘post‘, //data:{‘name‘:$("#name").val(),‘csrfmiddlewaretoken‘:$(‘[name="csrfmiddlewaretoken"]‘).val()}, data:{‘name‘:$("#name").val(),‘csrfmiddlewaretoken‘:‘{{ csrf_token }}‘}, success:function (data) { console.log(data) } }) }) </script> </html>
三:扩展默认的auth_user表
标签:tor action create close family round charset 开发 port
原文地址:https://www.cnblogs.com/xujinjin18/p/9665091.html