加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

实战django(二)--登录实现记住我

发布时间:2020-12-15 17:07:58 所属栏目:大数据 来源:网络整理
导读:上节初步实现了登录和注册模块,这节我们进一步实现“记住我”功能。 大体功能分为以下模块: 1.在登录时如果勾选记住我,那么就将用户username存进cookie中,跳转到index页面; 2.此时,如果不进行登出,如果直接返回登录页面,那么就会判断cookie中是否存

上节初步实现了登录和注册模块,这节我们进一步实现“记住我”功能。

大体功能分为以下模块:

1.在登录时如果勾选记住我,那么就将用户username存进cookie中,跳转到index页面;

2.此时,如果不进行登出,如果直接返回登录页面,那么就会判断cookie中是否存在username,如果存在,就直接跳转到Index界面,也就是不用再一次登录;

3.如果进行登出,则删除cookie中的username,并返回登录界面;

4.如果登录时不勾选记住我,那么跳转到index界面后,在访问登录界面不会继续跳转到Index界面;

5.若直接访问Index界面,会判断cookie中是否存在username,如果不存在,则会提示对不起,请先登录,三秒之后跳转到登录界面。

urls.py

from django.urls import path
from .  views

app_name='login'
urlpatterns=[
    path('',views.login,name=),path(register/',views.register,1)">registerregister/savereg/saveregtologin/tologinlogout/logoutindex/index

from django.shortcuts  render,redirect
from .models  Users
 datetime

# Create your views here.
def login(request):
    if request.get_signed_cookie(usernameSSS None:
        username = request.get_signed_cookie(None)
        return render(request,login/index.html: username})
    login/login.html)
 register(request):
    login/register.html)

 save_register(request):
    if request.method == POST:
        phone=request.POST.get(phone)
        username=request.POST.get()
        password=request.POST.get(password)
        repassword=request.POST.get(repassword)
        birthday=request.POST.get(birthday)
        errormsg=''
        if phone =='':
            errormsg=手机号不能为空'
        elif password ==密码不能为空else:
            if password!=repassword:
                errormsg=确认密码与密码不一致if errormsg == :
            Users.objects.create(phone=phone,username=username,password=password,birthday=birthday,regday=datetime.datetime.now().strftime(%Y-%m-%d))
            login/pagejump.html)
        errormsg:errormsg})

 to_login(request):
    )
        remember=request.POST.get(rememberif phone == ': 用户名不能为空})
        try:
            user_obj = Users.objects.get(phone=phone)
        except Users.DoesNotExist:
            ':账号不存在,请重新输入})
        pwd=user_obj.password
        password = request.POST.get(if password != :
            repwd=password
            if pwd == repwd:
                username=user_obj.username
                if remember is not None:
                    response = render(request,1)">:username})
                    response.set_signed_cookie("",username,max_age=60 * 60 * 12)
                    return response
                :
                    : username})
                 return render(request,'login/index.html',{'username':username})
            :
                密码错误})

 check_login(func):
    def helper(request,*args,**kwargs):
         None:
            return func(request,1)">kwargs)
        login/checklogin.html)
     helper

 logout(request):
    response = redirect(/login/")
    response.delete_cookie( response


@check_login
 index(request):
    ')
View Code

index.html

<!doctype html>
<html lang=en">
<head>
    <meta charset=UTF-8">
    <meta name=viewport
          content=width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0">
    <meta http-equiv=X-UA-Compatible" content=ie=edge">
    <title>Document</title>
</head>
<body>
    <h3><p>hello {{username}}</p></h3><span><p><a href={% url 'login:logout'%}">登出</a></p></span>
</body>
</html>
View Code

login.html

<!doctype html>
<html lang=">
    <title>盒老师</title>
    <meta name=keywords盒老师contentIE=edge,chrome=1">
    <link type=text/css" rel=stylesheet" href=/static/login/css/login.css">
    <script type=text/javascript" src=/static/login/js/jquery.min.js"></script>
</head>
<body class=login_bj" >
<div zhuce_body" style=position:absolute;left:130px;top:300px;">
    <div zhuce_kong login_kuang" >
        <div zc">
            <div bj_baiheight:330px;">
            <h3>登录</h3>
                   <form action={% url 'login:tologin'%}" method=post">
                <p style=color: red;margin-top: 0px;">{{errormsg}}</p>
                <input name=" type=text" kuang_txt" placeholder=手机号">
                <input name=密码">
                <div>
                       <input name=checkbox" value=" ><span>记住我</span><a href=#float:rigth;">忘记密码</a>
                </div>
                <div>
                       <p>没有账号?<a href={% url 'login:register'%}">立即注册</a></p>
                </div>
                <input style=margin-top:2px;" name=登录submitbtn_zhuce">
                </form>
            </div>
        </div>
    </div>

</div>
    
</body>
</html>
View Code

checklogin.html

<body>
<div align=centerborder: 1px solid blueviolet;
        position:absolute;left:500px;top: 200px;width: 430px;height: 50px;>
    <p style=text-align: center;font-size: 20px;">对不起!请先登录!还有<span id=sp">3</span>秒跳转到登录界面...</p>
</div>
<script>
    //onload事件会在页面加载完后立即发生
    onload=function () {
        //etInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
        setInterval(go,1000)
    };
    var x=2;
    function go() {

        if (x>=0){
            document.getElementById(").innerText=x;
        } {
            location.href={% url 'login:login'%};
        }
        x--;
    }
</script>
</body>
View Code

到这里,登录注册也基本告一段落了。同时我的django之旅也暂时告一段落了,花了接近一个月的时间,也算是有所收获,找时间会将所有代码上传至github。接下来准备去看自然语言处理了,极大可能是自己以后工作的内容。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读