zoukankan      html  css  js  c++  java
  • Django2 + ORM 做一个简单的登陆

    .
    ├── 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;  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;  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;  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

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

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

    此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935. 我的gitHub: (学习代码都在gitHub) https://github.com/nwgdegitHub/
  • 相关阅读:
    java 平面上最近两个点之间的距离
    java 选择排序
    java 复制字串算法
    mysql数据库的导入导出
    010editor安装包
    切换使用phpstudy的mysql和本机自带的mysql
    vmware安装kali
    win10,python安装mysql
    php变量等入门
    Wampserver 2.5安装
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/9365868.html
Copyright © 2011-2022 走看看