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')
代碼:
<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>
在這里插入圖片描述
*請認真填寫需求信息,我們會在24小時內與您取得聯系。