本站消息

  出租广告位,需要合作请联系站长

  今日名言-想象你自己对困难作出的反应,不是逃避或绕开它们,而是面对它们,同它们打交道,以一种进取的和明智的方式同它们奋斗 。——马克斯威尔·马尔兹

  今日名言-用谅解、宽恕的目光和心理看人、待人。人就会觉得葱笼的世界里,春意盎然,到处充满温暖。——蔡文甫


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

用python的django完成一个登录小案例

发布于2021-07-25 06:25     阅读(833)     评论(0)     点赞(10)     收藏(2)


1.urls.py文件中添加login(登录)和home(主页)的url配置。

image-20210723104231722

2.views.py代码

这里写的就是业务逻辑,写了两个方法。

其中request参数是必须的,拿的是浏览器拿交的相关信息。

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os.path


# Create your views here.
from django.shortcuts import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect


def index(request):
    return HttpResponse("CMDB")


def login(request):
    error_msg=""
    if request.method == "POST":
        username = request.POST.get("username",None)
        password = request.POST.get("password",None)
        print (username,password)
        if username=='root' and password=='admin123':
            #去跳转到
            return redirect('/home')
        else:
            error_msg='用户名或密码错误'
    return render(request, "login.html",{'error_msg':error_msg})


def home(request):
    return render(request,"home.html")

home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>李柏霖</td>
        <td></td>
        <td>123@187.com</td>
    </tr>    <tr>
        <td>李柏霖2</td>
        <td></td>
        <td>123@18754.com</td>
    </tr>    <tr>
        <td>李柏霖3</td>
        <td></td>
        <td>123@18786.com</td>
    </tr>
</table>
</body>
</html>

login.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/commons.css">
</head>
<style>
    label {
        width: 80px;
        text-align: right;
        display: inline-block;
    }

    #submit {
        margin-left: 100px;
        width: 100px;
        height: 30px;
    }
</style>
<body>
<form action="/login" method="post">
    <p>
        <label for="username">用户名:</label>
        <input type="text" name="username"><br>
    </p>
    <p>
        <label for="passoword">密码:</label>
        <input type="password" name="password">
        <span style="color: red">{{ error_msg }}</span>
        <br>
    </p>
    <input type="submit" id="submit">
</form>
<script src="/static/jquery-3.6.0.min.js"></script>
</body>
</html>

运行效果:

登录前:

image-20210723104950831

登录之后:

image-20210723104936798

原文链接:https://blog.csdn.net/qq_37924905/article/details/119026456



所属网站分类: 技术文章 > 博客

作者:天使是怎样炼成的

链接:http://www.pythonpdf.com/blog/article/484/e016bf91ba9cb6ad0c94/

来源:编程知识网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

10 0
收藏该文
已收藏

评论内容:(最多支持255个字符)