前面实现了用户登录,本篇简单介绍基于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)
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">
Title
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">
user_info.html:
html>
lang="en">
charset="UTF-8">
Title
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">
添加用户:
用户列表:
{% for row in user_list %}
href="/cmdb/userdetail-{{ row.id }}/">{{ row.username }}
|href="/cmdb/userdel-{{ row.id }}/">删除
|href="/cmdb/useredit-{{ row.id }}/">编辑
{% endfor %}
user_detail.html:
html>
lang="en">
charset="UTF-8">
Title
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">
Title
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">
编辑用户:
运行:
登录:
输入正确后:就可以做相应操作: