Django2 + ORM 做一个简单的登陆
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Django2 + ORM 做一个简单的登陆,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6946字,纯文字阅读大概需要10分钟。
内容图文
. ├── 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
直接登陆,随便输,没有做用户名和密码的验证
在管理界面可以新添加用户,删除,编辑
原文:https://www.cnblogs.com/liuw-flexi/p/9365868.html
内容总结
以上是互联网集市为您收集整理的Django2 + ORM 做一个简单的登陆全部内容,希望文章能够帮你解决Django2 + ORM 做一个简单的登陆所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。