前端代码 写一个登录的页面。用户名:输入框、密码:输入框居中对齐,布局合理。包括记住密码勾选框,登录按钮为浅蓝色。同时点击登陆时后端代码用django写出来,查询数据库。匹配成功后返回首页

时间: 2023-06-12 22:08:12 浏览: 50
HTML代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录页面</title> <style> body { background-color: #f6f6f6; font-family: Arial, sans-serif; } .form-wrapper { margin: 80px auto; width: 400px; background-color: #fff; padding: 40px; border-radius: 10px; box-shadow: 0px 0px 20px 0px rgba(0,0,0,0.2); } .form-wrapper h1 { font-size: 24px; margin-top: 0px; margin-bottom: 20px; text-align: center; } .form-group { margin-bottom: 15px; } .form-group label { display: block; font-size: 16px; margin-bottom: 5px; } .form-group input[type="text"], .form-group input[type="password"] { width: 100%; padding: 10px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 5px; outline: none; } .form-group input[type="checkbox"] { margin-right: 5px; vertical-align: middle; } .form-group .remember-me { font-size: 14px; display: inline-block; vertical-align: middle; } .btn-login { display: block; width: 100%; padding: 10px 15px; background-color: #28a745; color: #fff; font-size: 16px; text-align: center; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease-in-out; } .btn-login:hover { background-color: #218838; } </style> </head> <body> <div class="form-wrapper"> <h1>登录</h1> <form action="{% url 'login' %}" method="POST"> {% csrf_token %} <div class="form-group"> <label for="id_username">用户名:</label> <input type="text" name="username" id="id_username" required> </div> <div class="form-group"> <label for="id_password">密码:</label> <input type="password" name="password" id="id_password" required> </div> <div class="form-group"> <input type="checkbox" name="remember_me" id="id_remember_me"> <label for="id_remember_me" class="remember-me">记住密码</label> </div> <button type="submit" class="btn-login">登录</button> </form> </div> </body> </html> ``` 后端代码(使用Django): ```python from django.shortcuts import render from django.contrib.auth import authenticate, login from django.http import HttpResponseRedirect def login_view(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') remember_me = request.POST.get('remember_me') user = authenticate(request, username=username, password=password) if user is not None: login(request, user) if not remember_me: request.session.set_expiry(0) return HttpResponseRedirect('/') else: error_message = '用户名或密码错误,请重新输入' return render(request, 'login.html', {'error_message': error_message}) else: return render(request, 'login.html') ``` 其中,`login.html`是前端页面的文件名,`login_view`是Django中的视图函数,表示登录页面的访问路径。在POST请求中,获取用户输入的用户名、密码和是否勾选“记住密码”复选框的值,通过`authenticate`方法验证用户名和密码是否匹配,如果匹配则调用`login`方法进行登录,并根据是否勾选“记住密码”复选框设置session过期时间。如果验证失败,则返回错误信息并重新渲染登录页面。在GET请求中,直接渲染登录页面。

相关推荐

最新推荐

recommend-type

css中使input输入框与img(图片)在同一行居中对齐

如何让input输入框与img在同一行居中对齐,由于经常会使用到,默认情况下将input和img放同一行,img标签总是比input高出一个头,很难看,大家可以参考下本文或许有所帮助
recommend-type

微信小程序6位或多位验证码密码输入框功能的实现代码

主要介绍了微信小程序6位或多位验证码密码输入框功能的实现代码,实现思路很简单,需要的朋友可以参考下
recommend-type

Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法

主要介绍了Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法,需要的朋友可以参考下
recommend-type

智能制造的数字化工厂规划qytp.pptx

智能制造的数字化工厂规划qytp.pptx
recommend-type

罗兰贝格:德隆人力资源管理体系gltp.pptx

罗兰贝格:德隆人力资源管理体系gltp.pptx
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。