二十五 .Django---------auth认证组件

一. Django------auth认证组件

https://blog.csdn.net/huangql517/article/details/81381163    Django学习之用户认证组件

https://www.cnblogs.com/liwenzhou/p/9030211.html   认证组件

https://www.cnblogs.com/2mei/p/9217109.html       DJANGO用户登录验证完整代码

 https://download.csdn.net/download/baobao267/10722491  Django之Form表单验证及Ajax验证方式汇总

https://download.csdn.net/download/baobao267/10722491 Django之Form表单验证及Ajax验证方式汇总
https://blog.csdn.net/HFZeng/article/details/98654307
https://blog.csdn.net/kkorkk/article/details/80150644
https://www.jb51.net/article/165394.htm
https://segmentfault.com/q/1010000009345281
https://blog.csdn.net/huangql517/article/details/81259011

 

1. 实例

views

from
django.shortcuts import render, redirect, HttpResponse # Create your views here. from django.contrib import auth from django.contrib.auth.decorators import login_required from app01 import models from functools import wraps # def check_login(f): # @wraps(f) # def inner(request, *args, **kwargs): # if request.session.get("is_login") == "1": # return f(request, *args, **kwargs) # else: # return redirect("/login/") # return inner # # # def login(request): # if request.method == "POST": # username = request.POST.get("username") # password = request.POST.get("password") # # user = models.User.objects.filter(username=username, password=password) # [User Obj, ] # if user: # # 登陆成功 # request.session["is_login"] = "1" # # request.session["username"] = username # request.session["user_id"] = user[0].id # # 1. 生成特殊的字符串 # # 2. 特殊字符串当成key,在数据库的session表中对应一个session value # # 3. 在响应中向浏览器写了一个Cookie Cookie的值就是 特殊的字符串 # # return redirect("/index/") # # return render(request, "login.html") # # # @check_login # def index(request): # user_id = request.session.get("user_id") # # 根据id去数据库中查找用户 # user_obj = models.User.objects.filter(id=user_id) # if user_obj: # return render(request, "index.html", {"user": user_obj[0]}) # else: # return render(request, "index.html", {"user": "匿名用户"}) def login(request): if request.method == "POST": username = request.POST.get("username") pwd = request.POST.get("password") # 如何判断用户名和密码对不对 user = auth.authenticate(username=username, password=pwd) if user: ret = user.is_authenticated() print(ret) print("-" * 120) # 将登录的用户封装到request.user auth.login(request, user) return redirect("/index/") return render(request, "login.html") @login_required def index(request): print(request.user.username) print("=" * 120) ret = request.user.is_authenticated() print(ret) return render(request, "index.html") def logout(request): auth.logout(request) return redirect("/login/") # def register(request): # from django.contrib.auth.models import User # # # User.objects.create(username="alex", password="alexdsb") # 不用这个 # # User.objects.create_superuser() # user_obj = User.objects.create_user(username="alex5", password="alexdsb") # # 校验密码是否正确 # ret = user_obj.check_password("alex1234") # print(ret) # # 修改密码 # user_obj.set_password("alex3714") # user_obj.save() # return HttpResponse("o98k") def register(request): # User.objects.create(username="alex", password="alexdsb") # 不用这个 # User.objects.create_superuser() user_obj = models.UserInfo.objects.create_user(username="alex5", password="alexdsb") # 校验密码是否正确 ret = user_obj.check_password("alex1234") print(ret) # 修改密码 user_obj.set_password("alex3714") user_obj.save() return HttpResponse("o98k")
urls


from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^login/$', views.login),
    url(r'^index/$', views.index),
    url(r'^logout/$', views.logout),
    url(r'^reg/$', views.register),
]
models

from django.db import models
from django.contrib.auth.models import User, AbstractUser
# Create your models here.

# class User(models.Model):
#     username = models.CharField(max_length=16)
#     password = models.CharField(max_length=32)

#
# class UserDetail(models.Model):
#     phone = models.CharField(max_length=11)
#
#     user = models.OneToOneField(to=User)


class UserInfo(AbstractUser):
    phone = models.CharField(max_length=11)
    addr = models.CharField(max_length=128)
settings


# 如果没有登录,默认跳转到那个URL
LOGIN_URL = "/login/"
# 如果使用继承的方式 使用内置的auth模块,要在settings.py里面配置 默认用户认证时使用的是哪张表
AUTH_USER_MODEL = 'app01.UserInfo'
index模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>

<h1>这是index页面!</h1>

{#<p>欢迎:{{ user.username }} {{ user.password }}</p>#}


<a href="/logout/">注销</a>
</body>
</html>
login模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>

<h1>欢迎登录!</h1>
<form action="/login/" method="post">
    {% csrf_token %}

    <p>
        用户名:
        <input type="text" name="username">
    </p>
    <p>
        密码:
        <input type="password" name="password">
    </p>
    <p>
        <input type="submit" value="登录">
    </p>
</form>
</body>
</html>

 

转载于:https://www.cnblogs.com/lovershowtime/p/11384744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值