https://www.jianshu.com/p/bd0af02e59bacss
作一個簡單的數據庫交換的練習案例html
(1)建立django
(2)建立app文件python mange.py startapp cmdb
(3)建立數據庫,在project同名的配置的 init.py文件中配置mysql鏈接python
import pymysql pymysql.install_as_MySQLdb()
(4)在setting.py 中配置mysql 鏈接,找到DATABASESmysql
DATABASES = {
'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'testuser', 'USER':'root', 'PASSWORD':'root', 'HOST':'localhost', 'PORT': '3306', } }
(5)在setting文件下配置INSTALLED_APPS加入cmdb模塊sql
INSTALLED_APPS = [
'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'cmdb', ]
(6)根據CODEFIRST建立表,在app models.py 建立類數據庫
from django.db import models # Create your models here. class user_info(models.Model): username = models.CharField(max_length=32) password = models.CharField(max_length=64)
(7)建立新的遷移策略文件python manage.py makemigrations
(8)生成數據庫表python manage.py migrate
django
(1)在project 文件的url配置,url分發,分發到指定的appbash
from django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'cmdb/', include('cmdb.urls')) ]
(2)在指定的app文件下建立urls.py文件session
from django.conf.urls import url,include from cmdb import views urlpatterns = [ #登錄url url(r'login', views.login), #主界面展現url url(r'index', views.index), #展現所用戶信息url url(r'user_info', views.user_info), #展現我的信息的url url(r'user-(?P<nid>\d+)',views.user_per), #刪除我的信息的url url(r'delete-(?P<nid>\d+)',views.user_delete), ]
4、views 層邏輯編寫
(1)登錄主要用到了models.user_info.objects.filter(username=u, password=p).first()
app
def login(request): if request.method == 'GET': return render(request,'login.html',{'msg':''}) elif request.method == 'POST': u = request.POST.get('user',None) p = request.POST.get('pwd',None) if u and p : #select * from cmdb_user_info where username=u password=p obj = models.user_info.objects.filter(username=u, password=p).first() if obj: #重定向到cmdb/index url 上,url分發到index方法上 return redirect('/cmdb/index') else: msg = '用戶名密碼錯誤' return render(request,'login.html',{'msg':msg}) else: return render(request, 'login.html', {'msg': ''})
(2)主頁面展現
def index(request): return render(request,'index_u.html')
(3)用戶信息的增長/展現
主要用到了
#select * from cmdb_user_info obj = models.user_info.objects.all() #inster into cmdb_user_info(username,password) values(u,p) models.user_info.objects.create(username=u,password=p)
def user_info(request): if request.method == 'GET': #select * from cmdb_user_info obj = models.user_info.objects.all() return render(request,'index.html',{'user':obj}) elif request.method == 'POST': u = request.POST.get('user') p = request.POST.get('pwd') #inster into cmdb_user_info(username,password) values(u,p) models.user_info.objects.create(username=u,password=p) return redirect('/cmdb/user_info')
(4)刪除
主要用到
#刪除 delete from 表 where id=2 obj = models.user_info.objects.filter(id=nid).delete()
def user_delete(request, nid): #刪除 delete from 表 where id=2 obj = models.user_info.objects.filter(id=nid).delete() return redirect('/cmdb/user_info')
4、templates
(1)login頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="{{ request.path_info }}" method="post"> <input type="text" name="user"> <input type="password" name="pwd"> <span>{{ msg }}</span> <input type="submit"> </form> </body> </html>
(2)index 頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body{ margin: 0; } .header{ height: 48px; background-color: aquamarine; color: white; } .conleft{ position: absolute; top: 48px; width: 200px; left: 0; bottom: 0; background-color:chocolate; } .conright{ position: absolute; left: 200px; bottom: 0px; right: 0px; top: 48px; overflow: auto; background-color: burlywood; } </style> </head> <body> <div class="header">歡迎</div> <div class="con"> <div class="conleft"> <a href="/cmdb/user_info">用戶管理</a> </div> <div class="conright"> </div> </div> <div></div> </body> </html>
(3)用戶信息展現頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body{ margin: 0; } .header{ height: 48px; background-color: aquamarine; color: white; } .conleft{ position: absolute; top: 48px; width: 200px; left: 0; bottom: 0; background-color:chocolate; } .conright{ position: absolute; left: 200px; bottom: 0px; right: 0px; top: 48px; overflow: auto; background-color: burlywood; } </style> </head> <body> <div class="header">歡迎</div> <div class="con"> <div class="conleft"> <a href="/cmdb/user_info">用戶管理</a> </div> <div class="conright"> <form action="{{ request.path_info}}" method="post"> <input type="text" name="user"> <input type="text" name="pwd"> <input type="submit" > </form> {% for i in user %} <a href="/cmdb/user-{{ i.id }}">{{ i.username }} <a href="/cmdb/delete-{{ i.id }}">刪除</a> <br> {% endfor %} </div> </div> <div></div> </body> </html>
(4)我的信息更改頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body{ margin: 0; } .header{ height: 48px; background-color: aquamarine; color: white; } .conleft{ position: absolute; top: 48px; width: 200px; left: 0; bottom: 0; background-color:chocolate; } .conright{ position: absolute; left: 200px; bottom: 0px; right: 0px; top: 48px; overflow: auto; background-color: burlywood; } </style> </head> <body> <div class="header">歡迎</div> <div class="con"> <div class="conleft"> <a href="/cmdb/user_info">用戶管理</a> </div> <div class="conright"> <p>用戶信息</p> <form action="{{ request.path_info }}" method="post"> <input type="text" value="{{ i.username }}" name="user">- <input type="text" value="{{ i.password }}" name="pwd"> <input type="submit">編輯</a> </form> </div> </div> <div></div> </body> </html>