码迷,mamicode.com
首页 > 其他好文 > 详细

Django2 + ORM 做一个简单的登陆

时间:2018-07-25 15:13:03      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:meta   request   word   dmi   run   put   sqlite3   objects   span   

.
├── db.sqlite3
├── manage.py
├── myormLogin
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── ormlogin
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-36.pyc
    │   ├── admin.cpython-36.pyc
    │   ├── models.cpython-36.pyc
    │   ├── urls.cpython-36.pyc
    │   └── views.cpython-36.pyc
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   ├── 0001_initial.py
    │   ├── 0002_auto_20180724_0924.py
    │   ├── __init__.py
    │   └── __pycache__
    │       ├── 0001_initial.cpython-36.pyc
    │       ├── 0002_auto_20180724_0924.cpython-36.pyc
    │       └── __init__.cpython-36.pyc
    ├── models.py
    ├── templates
    │   ├── index.html
    │   ├── login.html
    │   ├── user_detail.html
    │   ├── user_edit.html
    │   └── user_info.html
    ├── tests.py
    ├── urls.py
    └── views.py

 

项目名/urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path(admin/, admin.site.urls),
    path(cmdb/,include("ormlogin.urls"))
]

 

app/urls.py

from django.urls import path
from ormlogin import views

urlpatterns = [
path(login/,views.login),
path(index/, views.index),
path(user_info/,views.user_info),
path(userdetail/<int:nid>/,views.user_detail),
path(userdel/<int:nid>/,views.user_del),
path(useredit/<int:nid>/,views.user_edit),
]

 

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)

 

views.py

from django.shortcuts import render, HttpResponse, redirect
from ormlogin import models


# Create your views here.

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 == None:
            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):
    obj = 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/)

 

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
     <div>
           欢迎您!
     </div>

     <div>
         <a class="menu" href="/cmdb/user_info/">管理1</a>
         <a class="menu" href="/cmdb/user_group/">管理2</a>
         <a class="menu">管理3</a>
     </div>
</body>
</html>

 

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</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_detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<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>
</head>
<body>
<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/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>
</body>
</html>

 

user_info.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<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/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>
</body>
</html>

 

python3 manage.py runserver 跑起来

 

浏览器输入地址:

127.0.0.1:8000/cmdb/login

 

直接登陆,随便输,没有做用户名和密码的验证

 

在管理界面可以新添加用户,删除,编辑

 

Django2 + ORM 做一个简单的登陆

标签:meta   request   word   dmi   run   put   sqlite3   objects   span   

原文地址:https://www.cnblogs.com/liuw-flexi/p/9365868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!