三、2:使用orm製作表圖的增、刪、改、查
阿新 • • 發佈:2018-12-03
一、首先配置orm
1、首先在settings.py中配置(資料庫需要手動提前先建好)
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'orm_db1', 'HOST':'127.0.0.1', 'PORT':3306, 'USER':'root', 'PASSWORD':'mariadb.123', } }
2、在__init__.py中新增以下內容:
import pymysql pymysql.install_as_MySQLdb()
二、在專案中配置
1、在models.py中建立使用者表與欄位填寫
from django.db import models class User(models.Model): id=models.AutoField(primary_key=True) name=models.CharField(max_length=32) password=models.CharField(max_length=32) address=models.CharField(max_length=64) phone=models.CharField(max_length=64,default='1308858585')
2、在總路由新增以後可能要新增的功能路由地址與對應的檢視
from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^user_list/',views.user_list), url(r'^delete/',views.delete), url(r'^add/',views.add), url(r'^edit/',views.edit), ]
3、在檢視功能檔案中填寫:views.py
from django.shortcuts import render,HttpResponse,redirect from app01 import models # Create your views here. def user_list(request): if request.method=='GET': # 查詢user表中所有資料.--相當於sql select * from app01_user; # 返回結果是QuerySet物件(先當成列表) # [user1,user2] ret=models.User.objects.all() # print(type(ret)) # for i in ret: # print(type(i)) # print(i.name) return render(request,'user_list.html',{'user_list':ret}) def delete(request): if request.method=='GET': id=request.GET.get('id') ret=models.User.objects.filter(id=id).delete() return redirect('/user_list/') def add(request): if request.method=='GET': return render(request, 'add.html') elif request.method=="POST": name=request.POST.get('name') pwd=request.POST.get('password') addr=request.POST.get('addr') phe=request.POST.get('phone_a') # 方式一 # user=models.User(name=name,password=pwd,address=addr) # user.save() #寫入資料庫 # 方式二 models.User.objects.create(name=name,password=pwd,address=addr,phone=phe) return redirect('/user_list/') def edit(request): if request.method=='GET': id=request.GET.get('id') user=models.User.objects.filter(id=id).first() return render(request,'edit.html',{'user':user}) if request.method=='POST': id=request.POST.get("id") name = request.POST.get('name') pwd = request.POST.get('password') addr = request.POST.get('addr') phe = request.POST.get('phone_a') models.User.objects.filter(id=id).update(name=name,password=pwd,address=addr,phone=phe) return redirect('/user_list/')
4、在templates目錄中建立以下html檔案
adduser.html
updateuser.html
userlist.html
adduser.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>新增使用者</title> </head> <body> <form action="" method="post"> <p>使用者名稱: <input type="text" name="name"></p> <p>密碼: <input type="password" name="password"></p> <p>地址: <input type="text" name="addr"></p> <input type="submit" value="提交"> </form> </body> </html>
updateuser.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/updateuser/?id={{ user.id }}" method="post"> <p><input type="hidden" name="id" value="{{ user.id }}"></p> <p>使用者名稱: <input type="text" name="name" value="{{ user.name }}"></p> <p>密碼: <input type="text" name="password" value="{{ user.password }}"></p> <p>地址: <input type="text" name="addr" value="{{ user.address }}"></p> <input type="submit" value="提交"> </form> </body> </html>
userlist.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>name</th> <th>password</th> <th>addr</th> <th>刪除</th> <th>編輯</th> </tr> </thead> <tbody> {% for user in user_list %} <tr> <td>{{ user.id }}</td> <td>{{ user.name }}</td> <td>{{ user.password }}</td> <td>{{ user.address }}</td> <td><a href="/deleteuser?id={{ user.id }}">刪除</a></td> <td><a href="/updateuser?id={{ user.id }}">編輯</a></td> </tr> {% endfor %} </tbody> <a href="/adduser/">新增使用者</a> </table> </body> </html>