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

相关推荐
今晚务必早点睡1 天前
MySQL 新手避坑指南:安装、区分、检查一步到位
数据库·mysql·adb
心本无晴.1 天前
拣学--基于vue3和django框架实现的辅助考研系统
vue.js·python·mysql·考研·django·dify
Darenm1111 天前
关于AI 面试官项目:智选ai 基于 Vue3 + Django + Dify 的全栈开发实战
人工智能·python·django
JIngJaneIL1 天前
基于java+ vue畅游游戏销售管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·游戏
詹姆斯爱研究Java1 天前
基于Django的租房网站的设计与实现
数据库·python·django
deng-c-f1 天前
Linux C/C++ 学习日记(50):连接池
数据库·学习·连接池
拉姆哥的小屋1 天前
基于多模态深度学习的城市公园社交媒体评论智能分析系统——从BERTopic主题建模到CLIP图文一致性的全栈实践
人工智能·python·深度学习·矩阵·媒体
ZAz_1 天前
DAY 41 图像数据与显存
python
曲幽1 天前
Python环境管理利器Conda:从入门到避坑实战指南
python·conda·pip·anaconda·uv·venv·miniconda
运维行者_1 天前
APM 性能监控是什么?从应用监控与网站监控了解基础概念
网络·数据库·云原生·容器·kubernetes·智能路由器·运维开发