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\d+)/', views.user_detail),
    url(r'^userdel-(?P\d+)/', views.user_del),
    url(r'^useredit-(?P\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:

html>
lang="en">

    charset="UTF-8">
    </span>Title<span style="color:#e8bf6a;">
    

style="margin: 0">
style="height: 48px; background-color: black;color: white"> 欢迎您!
style="position: absolute; top: 48px; bottom: 0; left:0; width: 200px; background-color: aqua"> class="menu" href="/cmdb/user_info/">管理1 class="menu" href="cmdb/user_group/">管理2 class="menu">管理3
style="position: absolute; top: 48px; left: 210px; bottom: 0;right: 0; overflow: auto">


login.html:

html>
lang="en">

    charset="UTF-8">


action="/cmdb/login/" method="POST">

type="text" name="user" placeholder="用户名"/>

type="text" name="pwd" placeholder="密码"/>

type="submit" value="提交"/>


user_info.html:

html>
lang="en">

    charset="UTF-8">
    </span>Title<span style="color:#e8bf6a;">
    

style="margin: 0">
style="height: 48px; background-color: black;color: white"> 欢迎您!
style="position: absolute; top: 48px; bottom: 0; left:0; width: 200px; background-color: aqua"> class="menu" href="/cmdb/user_info/">管理1 class="menu" href="cmdb/user_group/">管理2 class="menu">管理3
style="position: absolute; top: 48px; left: 210px; bottom: 0;right: 0; overflow: auto">

添加用户:

method="POST" action="/cmdb/user_info/"> type="text" name="user"/> type="text" name="pwd"/> type="submit" value="添加"/>

用户列表:



user_detail.html:

html>
lang="en">

    charset="UTF-8">
    </span>Title<span style="color:#e8bf6a;">
    

style="margin: 0">
style="height: 48px; background-color: black;color: white"> 欢迎您!
style="position: absolute; top: 48px; bottom: 0; left:0; width: 200px; background-color: aqua"> class="menu" href="/cmdb/user_info/">管理1 class="menu" href="cmdb/user_group/">管理2 class="menu">管理3
style="position: absolute; top: 48px; left: 210px; bottom: 0;right: 0; overflow: auto">

用户详细信息:

{{ obj.id }}
{{ obj.name }}
{{ obj.password }}


user_edit.html:

html>
lang="en">

    charset="UTF-8">
    </span>Title<span style="color:#e8bf6a;">
    

style="margin: 0">
style="height: 48px; background-color: black;color: white"> 欢迎您!
style="position: absolute; top: 48px; bottom: 0; left:0; width: 200px; background-color: aqua"> class="menu" href="/cmdb/user_info/">管理1 class="menu" href="cmdb/user_group/">管理2 class="menu">管理3
style="position: absolute; top: 48px; left: 210px; bottom: 0;right: 0; overflow: auto">

编辑用户:

method="POST" action="/cmdb/user_edit-{{ obj.id }}/"> style="display: none" type="text" name="id" value="{{ obj.id }}"/> type="text" name="username" value="{{ obj.username }}"/> type="text" name="password" value="{{ obj.password }}"/> type="submit" value="修改"/>




运行:

登录:

Django框架学习笔记(10.基于ORM实现简单的用户登录)_第1张图片

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

Django框架学习笔记(10.基于ORM实现简单的用户登录)_第2张图片


你可能感兴趣的:(Django框架)