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、成果演示

相关推荐
专注仿真37 分钟前
仿真智能体训练:强化学习中的生存、任务、杀伤、能耗与规则惩罚算法详解与实战
python·算法·afsim智能体·仿真训练·afsim智能体训练
FungLeo1 小时前
FengCMS · PHP+MySQL 升级实录 · 数据库驱动封装:mysqli 预处理与 affected_rows 关闭后变 -1 的坑
数据库·mysql·php·工程实践·fengcms
YYYing.2 小时前
【Redis剑指offer系列 (二) 】Redis线程模型
数据库·redis·缓存·八股
cpolar技术支持2 小时前
datetime.now() 和 datetime.now(timezone.utc) 混进 SQLite:零报错,读出来差 8 小时
python·sqlite·时间戳·datetime·时区
维爱智VR认知学能2 小时前
Python SQLite本地训练记录表迁移失败回滚与事务验证
数据库·python·单元测试·sqlite·软件工程
OpenCSG3 小时前
客户案例|Dell携手OpenCSG打造AI原生超节点
数据库·人工智能·ai-native
TDengine (老段)3 小时前
TDengine IDMP ---- Excel 插件
大数据·数据库·excel·时序数据库·tdengine·涛思数据
用户298698530143 小时前
使用 Python 替换 Word 文档文字:从全文到指定范围
python·api
Thanks_ks3 小时前
手写一个 LRU 缓存:哈希表与双向链表的组合设计
python·哈希表·lru·双向链表·ordereddict·命中率·哨兵节点
djjjx.3 小时前
Mysql数据库基础篇(十三):图解 MySQL 索引底层原理与事务隔离级别
数据库·mysql