1、创建登陆函数
def login(request):
if request.POST:
username = password = ''
username = request.POST.get('username')
password = request.POST.get('password')
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
auth.login(request, user)
request.session['username'] = username
response = HttpResponseRedirect('/home/')
return response
else:
return render(request, 'login.html', {'error': 'username or password error'})
return render(request, 'login.html')
def home(request):
return render(request, 'home.html')
2、创建向导
path('home/', views.home)
3、添加页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>自动化测试平台</title>
</head>
<body>
<ul class = "nav navbar-nav navbar-right" >
<li>欢迎,<a href="#">{{ user }}</a> </li>
<li><a href="/logout/">退出</a> </li>
</ul>
</body>
</html>
4、优化前面的页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>login</title>
<style>
body{
text-align: center;
}
</style>
</head>
<body>
<h1>自动化平台测试</h1>
<h1>login</h1>
<form method="post" action="/login/">{% csrf_token %}
<br><a> 用户名:</a>
<input name="username" type="text" placeholder="test">
<br><br><a> 密 码</a>
<br><input name="password" type="password" placeholder="你的密码"><br>
{{error}}<br>
<br><button style="width: 220px;height: 28px" name ="submit" type="submit">登陆</button>
</form>
</body>
</html>
5、成果演示
