python(Django)之登录功能实现

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>&nbsp;&nbsp;用户名:</a>
    <input name="username" type="text" placeholder="test">
    <br><br><a>&nbsp;&nbsp;密&nbsp;&nbsp;码</a>
    <br><input name="password" type="password" placeholder="你的密码"><br>
    {{error}}<br>&nbsp;
    <br><button style="width: 220px;height: 28px" name ="submit" type="submit">登陆</button>

</form>
</body>
</html>

5、成果演示

相关推荐
ai_top_trends5 分钟前
2026 年工作计划 PPT 横评:AI 自动生成的优劣分析
人工智能·python·powerpoint
TDengine (老段)14 分钟前
TDengine Python 连接器进阶指南
大数据·数据库·python·物联网·时序数据库·tdengine·涛思数据
赵渝强老师23 分钟前
【赵渝强老师】OceanBase的配置文件与配置项
数据库·oceanbase
brent42339 分钟前
DAY50复习日
开发语言·python
万行1 小时前
机器学习&第三章
人工智能·python·机器学习·数学建模·概率论
Data_agent1 小时前
Cocbuy 模式淘宝 / 1688 代购系统(欧美市场)搭建指南
开发语言·python
玖日大大1 小时前
OceanBase SeekDB:AI 原生数据库的技术革命与实践指南
数据库·人工智能·oceanbase
m0_726365831 小时前
哈希分分预测系统 打造自适应趋势分析「Python+DeepSeek+PyQt5」
python·qt·哈希算法
vyuvyucd1 小时前
Qwen-1.8B-Chat昇腾Atlas800TA2部署实战
python
轻竹办公PPT1 小时前
2026 年工作计划 PPT 内容拆解,对比不同 AI 生成思路
人工智能·python·powerpoint