from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse, render
def tool(sql,*args):
import pymysql
conn = pymysql.connect(host='127.0.0.1', user='root', password='456', db='db2', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute(sql)
res = cursor.fetchall()
print(res)
return res
def index(request):
sql = 'select * from userInfo'
userInfo = tool(sql)
return render(request,'index.html',{'userinfo': userInfo})
def login(request):
if request.method == 'GET':
return render(request, 'login.html')
else:
#list 套 dict
sql = 'select * from userInfo'
userInfo = tool(sql)
for user in userInfo:
if (user.get('name') == request.POST.get('username')) and (user.get('pwd') == request.POST.get('password')):
return render(request, 'index.html')
return render(request,'login.html',{'erroinfo': '用戶名或密碼錯誤'})
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', index),
url(r'^login/', login),
url(r'^', login),
]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用戶登陸--BBS</title>
<link rel="stylesheet" href="../static/css/reset.css">
<style>
body{
background-color: #e0e4e8;
}
.main{
position: absolute;
top: 80px;
width: 300px;
left: calc(35%);
border: 1px solid #eeeeee;
box-shadow: 0 0 2px #cccccc;
padding: 40px 60px 0 60px;
background-color: #ffffff;
}
.header h2{
text-align: center;
}
.header a{
display: block;
width: 100px;
height: 100px;
border-radius: 50%;
background-image: url("../static/image/微信圖片_20190613085655.png");
margin: 10px auto 30px auto;
}
.middle form input{
width: 300px;
border: 1px solid #ffffff;
box-shadow: 0 0 1px #cccccc;
margin-bottom: 10px;
}
.middle form a{
display: block;
font-size: 12px;
color: cornflowerblue;
padding-left: 240px;
user-select: none;
margin-bottom: 30px;
}
.middle form button{
width: 80px;
background-color: cornflowerblue;
color: white;
border: none;
border-radius: 3px;
margin: 20px 110px 70px ;
}
.erro{
height: 10px;
font-size: 10px;
font-weight: 400;
color: red;
text-align: center;
}
</style>
</head>
<body>
<div class="main">
<div class="header">
<h2>用戶登陸</h2>
<a></a>
</div>
<div class="middle">
<form action="/login/" method="post">
<input type="text" name="username" placeholder="登陸用戶名">
<a>忘記用戶名</a>
<input type="password" name="password" placeholder="密碼">
<a>忘記密碼</a>
<h6 class="erro">{{erroinfo}}</h6>
<button type="submit" value="登陸">登陸</button>
</form>
</div>
</div>
</body>
</html>