整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

Django系统判断用户是否登录的方法

一种判断用户是否登录

class UserCenterView(View):
    # 第一种判断用户是否登录
    def get(self, request):
        # 判断用户是否登录
        if request.user.is_authenticated:
            return render(request, 'user_center.html')
        else:
            response = redirect('/login/')
            response.delete_cookie("username")
            return response

第二种判断用户是否登录

# urls.py 文件
from django.contrib.auth.decorators import login_required
url(r'^info/$', login_required(views.UserCenterView.as_view()), name="center"),

view.py
class UserCenterView(View):
	
    # 第一种判断用户是否登录
    def get(self, request):
        # 判断用户是否登录
        return render(request, 'user_center.html')

setting.py
# 定义未登录跳转页面
LOGIN_URL = '/login/'

第三种判断用户是否登录

from django.contrib.auth.mixins import LoginRequiredMixin
class UserCenterView(LoginRequiredMixin,View):
	login_url='/login/'
    # 第一种判断用户是否登录
    def get(self, request):
        # 判断用户是否登录
        return render(request, 'user_center.html')

第四种判断用户是否登录(自己封装View)

代码:

<html>

<head>

<title>if语句验证用户登录</title>

</head>

<body>

<table width="250" height="50" align="center" border="1">

<form name="form1" method="post" action="">

<tr>

<td bgcolor="#eeeeee" bordercolor="#FFFFFF" bordercolordark="#CCCCCC" bordercolorlight="#FFFFFF"colspan="2" align="center">用户登录</td>

</tr>

<tr>

<td width="150" align="center" valign="middle" >用户名:</td>

<td width="50"><input name="yan" type="text" maxlength="20"></td>

</tr>

<tr>

<td align="center">密 码:</td>

<td><input name="pass" type="password" maxlength="10"></td>

</tr>

<tr>

<td colspan="2"align="center"><input type="button" value="登陆" onClick="check()">

<input type="reset" value="重置">

</td>

</tr>

</table>

</body>

</html>

<script language="javascript">

function check(){

if(form1.yan.value==""){

alert("请输入用户名!");form1.yan.foucs();return;

}else if(form1.pass.value==""){

alert("请输入密码!");form1.pass.foucs();return;

}

else{

form1.submit();

}

}

</script>

运行结果:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    /*总体的样式*/
    <style>
    	/*盒子样式*/
        #box{
            width: 350px; //宽
            height: 450px; //高
            border: 1px solid black; //边框
            border-radius: 10px; //边框弧度
            font-family: 黑体; //字体
            letter-spacing:8px; //段间距
            word-spacing: 10px; //字间距
            line-height: 40px; //行高
            font-size: 18px; //字大小
            padding: 20px; //内边框
        }
        /*给'注册'赋予样式*/
        .register{
            width:280px ; //宽
            height: 50px; //高
            background-color: skyblue; //背景颜色
            border-radius: 10px; //边框弧度

        }
        /*将所有边框都改变*/
        *{
            border-radius: 5px; 边框弧度
        }
        /*使用class选择器,赋予number宽高和边框*/
        .number{
            width: 185px; //宽
            height: 27px; //高
            border-width: 1px; //边框宽度

        }
        /*id选择器*/
        #two{
            width: 55px; //宽
            border-width: 1px; 边框宽度
        }
        /*id选择器*/
        #phone{
            width: 103px; //宽
        }
        /*class 选择器*/
        .boxs{
            zoom: 75%; //清除浮动
            color: darkgray; //颜色
        }
        /*class选择器*/
        .box_a{
            width: 50px; //宽
            height: 50px; //高
            background-image: url("../image/04.jpg "); //背景图片
            background-repeat: no-repeat; // 是否平铺
            background-size: 50px 25px; //背景尺寸
            position: relative; //定位 相对定位
            left: 310px; //定位后左移
            bottom: 32px; //定位后下移

        }
    </style>
</head>
<body>
<div id="box">
    <h1>请注册</h1>
<p style="color: darkgray">已有帐号?<a href="https://im.qq.com/index">登录</a></p>
<form action="" method="post">
    <label for="name">用户名</label>
    <input type="text" placeholder="请输入用户名" id="name" class="number"> <br>
    <label for="phone">手机号</label>
    <select name="" id="two" class="number">
    <optgroup>
        <option style="" class="">+86</option>
    </optgroup>
    </select>
    <input type="text" placeholder="请输入手机号" id="phone" class="number"> <br>
    <label for="mima">密 码</label>
    <input type="password" placeholder="请输入密码" id="mima" class="number"> <br>
    <label for="mima">验证码</label>
    <input type="password" placeholder="请输入验证码" id="is" class="number">
    <div class="box_a"></div>
    <div class="boxs">
        <input type="radio" id="" class="accept">阅读并接受协议<br>
    </div>
    <input type="submit" value="注册" class="register" >

    </form>
</div>


</body>
</html>
在这里插入图片描述