Django框架学习笔记(10.基于ORM实现简单的用户登录)

前面实现了用户登录,本篇简单介绍基于ORM的用户登录:


获取后数据验证的方法1:

u = request.POST.get('user')
p = request.POST.get('pwd')
obj = models.UserInfo.objects.filter(username=u, password=p).first()
if obj == None:


获取后数据验证的方法2(不常用):

obj = models.UserInfo.objects.filter(username=u, password=p).count()
if obj == 0:


创建Django工程,配置相关,创建app01

在project下的urls.py路由分流:

from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^cmdb/', include("app01.urls")),
]

app01下的models.py:

from django.db import models

# Create your models here.

class UserInfo(models.Model):
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=64)

app01下新建urls.py:

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)
]

app01下的views.py:

from django.shortcuts import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect
from app01 import models



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

def login(request):
    if request.method == "GET":
        return render(request, "login.html")
    elif request.method == "POST":
        u = request.POST.get('user')
        p = request.POST.get('pwd')
        obj = models.UserInfo.objects.filter(username=u, password=p).first()
        if obj:
            return redirect('/cmdb/index/')
        return render(request, "login.html")
    else:
        return redirect(request, "/index/")



def user_info(request):
    if request.method =="GET":
        user_list = models.UserInfo.objects.all()
        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()
    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/')


简单写几个html:

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .menu{
            display: block;
            padding: 5px;
        }
    </style>
</head>
<body style="margin: 0">
<div style="height: 48px; background-color: black;color: white">
    欢迎您!
</div>
<div style="position: absolute; top: 48px; bottom: 0; left:0; width: 200px; background-color: aqua">
    <a class="menu" href="/cmdb/user_info/">管理1</a>
    <a class="menu" href="cmdb/user_group/">管理2</a>
    <a class="menu">管理3</a>
</div>
<div style="position: absolute; top: 48px; left: 210px; bottom: 0;right: 0; overflow: auto">
</div>
</body>
</html>


login.html:

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


user_info.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .menu{
            display: block;
            padding: 5px;
        }
    </style>
</head>
<body style="margin: 0">
<div style="height: 48px; background-color: black;color: white">
    欢迎您!
</div>
<div style="position: absolute; top: 48px; bottom: 0; left:0; width: 200px; background-color: aqua">
    <a class="menu" href="/cmdb/user_info/">管理1</a>
    <a class="menu" href="cmdb/user_group/">管理2</a>
    <a class="menu">管理3</a>
</div>
<div style="position: absolute; top: 48px; left: 210px; 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>
</div>
</body>
</html>


user_detail.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .menu{
            display: block;
            padding: 5px;
        }
    </style>
</head>
<body style="margin: 0">
<div style="height: 48px; background-color: black;color: white">
    欢迎您!
</div>
<div style="position: absolute; top: 48px; bottom: 0; left:0; width: 200px; background-color: aqua">
    <a class="menu" href="/cmdb/user_info/">管理1</a>
    <a class="menu" href="cmdb/user_group/">管理2</a>
    <a class="menu">管理3</a>
</div>
<div style="position: absolute; top: 48px; left: 210px; bottom: 0;right: 0; overflow: auto">
    <h1>用户详细信息:</h1>
    <h5>{{ obj.id }}</h5>
    <h5>{{ obj.name }}</h5>
    <h5>{{ obj.password }}</h5>
</div>
</body>
</html>


user_edit.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .menu{
            display: block;
            padding: 5px;
        }
    </style>
</head>
<body style="margin: 0">
<div style="height: 48px; background-color: black;color: white">
    欢迎您!
</div>
<div style="position: absolute; top: 48px; bottom: 0; left:0; width: 200px; background-color: aqua">
    <a class="menu" href="/cmdb/user_info/">管理1</a>
    <a class="menu" href="cmdb/user_group/">管理2</a>
    <a class="menu">管理3</a>
</div>
<div style="position: absolute; top: 48px; left: 210px; bottom: 0;right: 0; overflow: auto">
    <h1>编辑用户:</h1>
    <form method="POST" action="/cmdb/user_edit-{{ 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>
</body>
</html>




运行:

登录:


输入正确后:就可以做相应操作:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值