前面实现了用户登录,本篇简单介绍基于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>
运行:
登录:
输入正确后:就可以做相应操作: