App01--->urls.py

from django.contrib import admin
from django.conf.urls import url
from app01 import views

urlpatterns = [
    url(r'^login/', views.login),
    url(r'^index/', views.index),
    url(r'^user_info/', views.user_info),
    url(r'^userdetail-(?P<nid>\d+)/', views.user_detail),
    url(r'^userdel-(?P<nid>\d+)/', views.user_del),
    url(r'^useredit-(?P<nid>\d+)/', views.user_edit),
    url(r'^orm/', views.orm),
]

Urls.py
from django.contrib import admin
from django.conf.urls import url,include

urlpatterns = [
    url(r'^cmdb/',include('app01.urls')),
    # url(r'^monitor/',include('app02.urls')),
]

App--->Views.py

from django.shortcuts import render,HttpResponse,redirect
from app01 import models
def orm(reqbuest):
    # 创建表
    # models.UserInfo.objects.create(username='root',password='123')
    # dic={'username':'eric','password':'666666'}
    # models.UserInfo.objects.create(**dic)
    # obj=models.UserInfo(username='root1',password='123456')
    # obj.save()

    # 查询表
    # result= models.UserInfo.objects.all()
    # result,QuerySet =>Django=>[]
    #[obj(id,username,password),obj(id,username,password),obj(id,username,password)]
    # result = models.UserInfo.objects.filter(username='root',password='123')
    # for row in result:
    #     print(row.id,row.username,row.password)

    #删除
    # models.UserInfo.objects.filter(id='2').delete()

    # 更改
    models.UserInfo.objects.filter(id='3').update(password='666666')
    return HttpResponse('orm')

def login(request):
    if request.method=="GET":
        return render(request,'login.html')

    elif request.method=="POST":
        # 数据库中执行 select * from user where username='x' and password='x'
        u=request.POST.get('user')
        p=request.POST.get('pwd')
        obj= models.UserInfo.objects.filter(username=u,password=p).first()
        # count = models.UserInfo.objects.filter(username=u, password=p).count()
        if obj:
            return redirect('/cmdb/index/')
        else:
            return render(request,'login.html')

    else:
        return redirect('/index/')

def index(request):
    return render(request,'index.html')

def user_info(request):
    if request.method=="GET":
        user_list= models.UserInfo.objects.all()
        # print(user_list.query)
        # QuerySet [obj,obj]
        return render(request,'user_info.html',{'User_List':user_list})
    elif request.method=="POST":
        u=request.POST.get('user')
        p=request.POST.get('pwd')
        models.UserInfo.objects.create(username=u,password=p)

        return redirect('/cmdb/user_info/')

def user_detail(request,nid):
    obj=models.UserInfo.objects.filter(id=nid).first()
    # models.UserInfo.objects.get(id=nid) 如果没有数据会报错
    return render(request,'user_detail.html',{'OBJ':obj})

def user_del(request,nid):
    models.UserInfo.objects.filter(id=nid).delete()
    return redirect('/cmdb/user_info/')

def user_edit(request,nid):
    if request.method=="GET":
        obj=models.UserInfo.objects.filter(id=nid).first()
        return render(request,'user_edit.html',{'OBJ':obj})
    elif request.method=="POST":
        nid=request.POST.get('id')
        u=request.POST.get('username')
        p=request.POST.get('password')
        models.UserInfo.objects.filter(id=nid).update(username=u,password=p)
        return redirect('/cmdb/user_info/')

App01--->models.py

from django.db import models

#app01_userinfo
class UserInfo(models.Model):
    # id列,自增,主键
    # 用户名列,字符串类型,指定长度
    username=models.CharField(max_length=64)
    password=models.CharField(max_length=64)

Tempalte--->login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/cmdb/login/" method="post" enctype="multipart/form-data">
        <p>
            <input type="text" name="user" placeholder="用户名">
        </p>
        <p>
            <input type="password" name="pwd" placeholder="密码">
        </p>
        <p>
            <input type="submit" value="提交">
        </p>
    </form>
</body>
</html>

Templates--->index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin:0;
        }
        .menu{
            display: block;
            padding: 5px;
        }
    </style>
</head>
<body>
    <div style="height: 48px;background-color: black;color: white;">
        
    </div>
    <div>
        <div style="position: absolute;top:48px;bottom: 0;left: 0;width: 500px;background-color: brown">
            <a class="menu" href="/cmdb/user_info/">用户管理</a>
            <a class="menu" href="/cmdb/user_group/">用户组管理</a>
        </div>
        <div style="position: absolute;top:48px;left:520px;bottom: 0;right: 0;overflow: auto">
            
        </div>
    </div>
    
</body>
</html>

Templates--->user_info.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin:0;
        }
        .menu{
            display: block;
            padding: 5px;
        }
    </style>
</head>
<body>
    <div style="height: 48px;background-color: black;color: white;">

    </div>
    <div>
        <div style="position: absolute;top:48px;bottom: 0;left: 0;width: 500px;background-color: brown">
            <a class="menu" href="/cmdb/user_info/">用户管理</a>
            <a class="menu" href="/cmdb/user_group/">用户组管理</a>
        </div>
        <div style="position: absolute;top:48px;left: 520px;bottom: 0;right: 0;overflow: auto">
            <h3>添加用户</h3>
            <form method="POST" action="/cmdb/user_info/">
                <input type="text" name="user">
                <input type="text" name="pwd">
                <input type="submit" value="添加">
            </form>
            <h3>用户列表</h3>
            <ul>
                {% for row in User_List %}
                    <li><a href="/cmdb/userdetail-{{ row.id }}/">{{ row.username }}</a> |
                        <a href="/cmdb/userdel-{{ row.id }}/">删除</a> |
                        <a href="/cmdb/useredit-{{ row.id }}/">编辑</a> |
                    </li>
                {% endfor %}
            </ul>
            <ul>

            </ul>

        </div>
    </div>

</body>
</html>

Templates--->user_edit.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin:0;
        }
        .menu{
            display: block;
            padding: 5px;
        }
    </style>
</head>
<body>
    <div style="height: 48px;background-color: black;color: white;">

    </div>
    <div>
        <div style="position: absolute;top:48px;bottom: 0;left: 0;width: 500px;background-color: brown">
            <a class="menu" href="/cmdb/user_info/">用户管理</a>
            <a class="menu" href="/cmdb/user_group/">用户组管理</a>
{#            <a class="menu"></a>#}
        </div>
        <div style="position: absolute;top:48px;left:520px;bottom: 0;right: 0;overflow: auto">
            <h1>编辑用户</h1>
                <form method="post" action="/cmdb/useredit-{{ OBJ.id }}/">
                    <input style="display: none" type="text" name="id" value="{{ OBJ.id }}"/>
                    <input type="text" name="username" value="{{ OBJ.username }}"/>
                    <input type="text" name="password" value="{{ OBJ.password }}"/>
                    <input type="submit" value="提交">
                </form>
        </div>
    </div>

</body>
</html>

Templates--->user_detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin:0;
        }
        .menu{
            display: block;
            padding: 5px;
        }
    </style>
</head>
<body>
    <div style="height: 48px;background-color: black;color: white;">

    </div>
    <div>
        <div style="position: absolute;top:48px;bottom: 0;left: 0;width: 500px;background-color: brown">
            <a class="menu" href="/cmdb/user_info/">用户管理</a>
            <a class="menu" href="/cmdb/user_group/">用户组管理</a>
        </div>
        <div style="position: absolute;top:48px;left: 520px;bottom: 0;right: 0;overflow: auto">
            <h1>用户详细信息</h1>
            <h5>id: {{ OBJ.id }}</h5>
            <h5>username: {{ OBJ.username }}</h5>
            <h5>password: {{ OBJ.password }}</h5>
        </div>
    </div>

</body>

Django实例(3)-用户连数据库登入系统的更多相关文章

  1. Linux显示目前登入系统的用户信息

    Linux显示目前登入系统的用户信息 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ who youhaidong :0 2015-01-26 22:43 (: ...

  2. Linux显示目前与过去登入系统的用户相关信息

    Linux显示目前与过去登入系统的用户相关信息 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ last youhaido pts/0 :0 Sat Jan 2 ...

  3. last 列出登入系统的用户相关信息

    Linux last 命令介绍 功能说明:列出目前与过去登入系统的用户相关信息. 语法:    last [-adRx][-f <记录文件>][-n <显示列数>][帐号名称. ...

  4. Centos-当前和过去登入系统用户信息-last

    last 获取当前和过去登入系统的用户相关信息,执行last指令的时候会默认读取/var/log/wtmp文件 相关参数 -a 把客户端IP显示到最后一列 -R 不显示客户端IP地址或主机名 -n 显 ...

  5. Linux显示登入系统的帐号名称和总人数

    Linux显示登入系统的帐号名称和总人数 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ who -q youhaidong youhaidong # 用户数= ...

  6. struts2自定义拦截器与cookie整合实现用户免重复登入

    目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...

  7. LINUX 无法登入系统(2017-1-16)

    很好的博文:http://blog.csdn.net/caizi001/article/details/38659189

  8. MYSQL数据库的安装,配置文件,登入

    07.13自我总结 MYSQL数据库 一.MYQL数据库的安装 可以去mysql官网下载mysql压缩包 运行程序:在bin文件夹中,其中客户端运行文件是mysql.exe,服务端运行文件为mysql ...

  9. QQ登入(6)腾讯微博-获取微博用户信息,发送微博

    1.1获取weibo用户信息 //先登入授权,可以参考QQ登入(1) Weibo mWeibo = new Weibo(this, mQQAuth.getQQToken()); mWeibo.getW ...

  10. [ASP.NET MVC] ASP.NET Identity登入技术应用

    [ASP.NET MVC] ASP.NET Identity登入技术应用 情景 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证.授权等等机制.在ASP.NET I ...

随机推荐

  1. 手把手教你更优雅的享受 DeepSeek

    开始之前,首先要确定你已经配置好Ollama软件并正常运行DeepSeek本地模型.如果这一步还不清楚,请翻看之前的手把手教程<手把手教你部署 DeepSeek 本地模型>. 本文是手把手 ...

  2. DOS批处理小案例应用分享 - 整理桌面

    想必大多数办公的同志都有个习惯----往桌面上堆放文件,各种文件.几天下来桌面就杂乱无章,乱七八糟的.能做一个快速清理的功能,是很有实用价值的.比如采用Windows自带的DOS批处理系统,就可快速搭 ...

  3. 基于 Flink+Iceberg 构建企业级实时数据湖

    Apache Flink 是大数据领域非常流行的流批统一的计算引擎,数据湖是顺应云时代发展潮流的新型技术架构.那么当 Apache Flink 遇见数据湖时,会碰撞出什么样的火花呢?本次分享主要包括以 ...

  4. VMware16虚拟机安装激活教程

    1.开始安装前需要准备好的软件 VMware-workstation-full-16--虚拟机软件(必要) 获取方式: 官方下载地址:https://www.vmware.com/cn/product ...

  5. Windows 提权-RunAs

    本文通过 Google 翻译 RunAs – Windows Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词进行了校正及个别注释补充. 导航 0 前言 ...

  6. 又一国产AI爆火!Manus强势炸场,邀请码申请方法,看这一篇就够了!

    3月6日凌晨,一款名为Manus的国产AI产品横空出世,迅速霸榜社交平台热搜.其内测邀请码在二手交易平台被炒至5万元天价,甚至出现标价10万元的卖家,我的个乖乖啊. 究竟是什么让Manus如此火爆?今 ...

  7. 朝花夕拾,帮三年前的自己改bug

    三年前,滨海之边马上毕业的老少年 经过几天半死不活的思考之后决定干前端 那个时候为了面试各种css属性js API背的是滚瓜烂熟 然后投简历,企业要项目经验, 我没有工作我哪来的项目经验啊 没人会管你 ...

  8. IvorySQL 4.0 之 Invisible Column 功能解析

    前言 随着数据库应用场景的多样化,用户对数据管理的灵活性和隐私性提出了更高要求.IvorySQL 作为一款基于 PostgreSQL 并兼容 Oracle 的开源数据库,始终致力于在功能上保持领先和创 ...

  9. MySQL-事务中的一致性读和锁定读的具体原理

    前言 上一篇文章MySQL-InnoDB行锁中,提到过一致性锁定读和一致性非锁定读,这篇文章会详细分析一下在事务中时,具体是如何实现一致性的. 一致性读原理 start transaction和beg ...

  10. 深入掌握FastAPI与OpenAPI规范的高级适配技巧

    title: 深入掌握FastAPI与OpenAPI规范的高级适配技巧 date: 2025/03/30 01:16:11 updated: 2025/03/30 01:16:11 author: c ...