#前言
- db browser或是django的後台,都能對資料表內容做編輯,
- django網頁裡,是使用ORM(Object Relational Mapping)的指令來操作sqlite資料表
舉例如下,
Create: table_name.objects.create()
Read: table_name.objects.all(), db.objects.get(), db.objects.filter()
Update: data_obj.update()
Delete: data_obj.delete()
data_obj.save()
- 這裡的資料庫已經利用createdata.py 建立,參見sqlite(三) 在django中建立
#網站:練習CRUD(點選功能後,請按GO)
#程式
- 各頁面說明:
index:首頁
page6是查詢
page7是新增,page9是新增後的結果。
page8是修改,page10是修改後的結果。
page5是刪除,page11是刪除後的結果。
models.py的主要內容
from django.db import models
# 資料表結構
class gasSite(models.Model):
gassiteName = models.CharField(max_length=20)
gassiteAddress = models.TextField(max_length=100)
gassiteOwner = models.TextField(max_length=10)
gassiteTelephone = models.CharField(max_length=20)
# 資料表結構
class gasSite(models.Model):
gassiteName = models.CharField(max_length=20)
gassiteAddress = models.TextField(max_length=100)
gassiteOwner = models.TextField(max_length=10)
gassiteTelephone = models.CharField(max_length=20)
urls.py的內容
from django.contrib import admin
from django.urls import path
from myapp.views import index,page1,page5,page6,page7,page8,page9,page10,page11
urlpatterns = [
path('admin/', admin.site.urls),
path('',index),
path('1/',page1),
path('5/',page5),
path('6/',page6),
path('7/',page7),
path('8/',page8),
path('9/',page9),
path('10/',page10),
path('11/',page11),
]
from django.urls import path
from myapp.views import index,page1,page5,page6,page7,page8,page9,page10,page11
urlpatterns = [
path('admin/', admin.site.urls),
path('',index),
path('1/',page1),
path('5/',page5),
path('6/',page6),
path('7/',page7),
path('8/',page8),
path('9/',page9),
path('10/',page10),
path('11/',page11),
]
views.py的內容
from django.shortcuts import render# 本程式的資料表
from myapp.models import gasSite
#test測試用
def searchOne(request):
theResult=gasSite.objects.get(id=3)
print(theResult)
return render(request,"list1.html",locals())
#test測試用
def showdata(request):
dbData=gasSite.objects.all()
return render(request,"index.html",locals())
#HOME PAGE
def index(request):
return render(request,"index.html")
#no use後來沒用上了
def page1(request):
pass
return render(request,"page1.html",locals())
#展示delete的頁面
def page5(request):
dbData=gasSite.objects.all()
return render(request,"page5.html",locals())
#列出資料表所有內容
def page6(request):
dbData=gasSite.objects.all()
return render(request,"page6.html",locals())
#展示新增資料的頁面
def page7(request):
pass
return render(request,"page7.html",locals())
#展示修改資料的頁面
def page8(request):
dbData=gasSite.objects.all()
return render(request,"page8.html",locals())
#展示新增資料完成的結果
def page9(request):
if request.method=="POST":
gname=request.POST['gname']
gaddress=request.POST['gaddress']
gowner=request.POST['gowner']
gtelephone=request.POST['gtelephone']
data1=gasSite.objects.create(gassiteName=gname,gassiteAddress=gaddress,gassiteOwner=gowner,gassiteTelephone=gtelephone)
data1.save()
#取出資料庫的最後一筆,用filter,避免有重複的name
newdata=gasSite.objects.filter(gassiteName=gname)
newdata=newdata[len(newdata)-1]
return render(request,"page9.html",locals())
#展示修改資料後的結果
def page10(request):
if request.method=="POST":
gid=request.POST['gid']
gname=request.POST['gname']
gaddress=request.POST['gaddress']
gowner=request.POST['gowner']
gtelephone=request.POST['gtelephone']
data1=gasSite.objects.filter(id=gid)
data1.update(gassiteName=gname,gassiteAddress=gaddress,gassiteOwner=gowner,gassiteTelephone=gtelephone)
return render(request,"page10.html",locals())
#展示刪除後的結果
def page11(request):
if request.method=="POST":
kid=request.POST['hvalue']
data1=gasSite.objects.get(id=kid)
data1.delete()
return render(request,"page11.html",locals())
沒有留言:
張貼留言