Django实例-用户连数据库登入系统

Posted leiwenbin627

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django实例-用户连数据库登入系统相关的知识,希望对你有一定的参考价值。

技术图片

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实例-用户连数据库登入系统的主要内容,如果未能解决你的问题,请参考以下文章

django中用户登入时初始化session中保存的数据

django中用户登入时初始化session中保存的数据

django项目 设置session 实现用户登入登出

Oracle数据库任何用户密码都能以sysdba角色登入

Django,COOKIES,SESSION完成用户登入

连接到 Django 后端的 AWS Postgres 实例