1. 程式人生 > >Django框架 models模組使用

Django框架 models模組使用

 

  

   #啟動資料庫(sqlite3)

 dos下:

  

  python manage.py makemigrations

  python manage.py migrate

 #views 

from django.shortcuts import render
from apppp.models import Book
from django.http import HttpResponse
# Create your views here.
def goindex(request):
return render(request,'index.html')



#新增資料
def addBook(request):
if request.POST:
#接收資料
name=request.POST.get('bname')
#寫入資料庫
book=Book(bname=name)
book.save()
return HttpResponse('<script>alert("新增成功")</script>')
else:
return render(request,'insert.html')

#查詢資料
def chaxun(request):

#查詢資料
res=Book.objects.all()
content={}
content['chaxunkey']=res
return render(request,'chaxun.html',content)

#根據指定欄位查詢資料
#根據編號查詢
def NOchaxun(request):
#接收資料
id=request.GET.get('id')
#查詢資料
res=Book.objects.get(bid=id)
content={}
content['NOchaxunkey']=res
return render(request,'NOchaxun.html',content)


#刪除業務(根據條件)
def deletedemo(request,id):
#刪除資料
Book.objects.filter(bid=id).delete()
return HttpResponse('<script>alert("刪除成功")</script>')

#修改
def xiugai(request,id,name):
#修改資料 Book.objects.filter(bid=id).update(bname=name) return HttpResponse('<script>alert("修改成功")</script>')




#models  
from django.db import models

# Create your models here.
#繼承models
class Book(models.Model):
#設定資料表字段
bid=models.AutoField(primary_key=True)
bname=models.CharField(max_length=30)


#urls
from django.contrib import admin
from django.urls import path
from apppp.views import goindex,addBook,chaxun,NOchaxun,deletedemo,xiugai

urlpatterns = [
path('admin/', admin.site.urls),
path('index/',goindex),
path('addBook/',addBook),
path('chaxun/',chaxun),
path('NOchaxun/',NOchaxun),
path('deletedemo/<int:id>/',deletedemo),
path('xiugai/<int:id>/<str:name>/',xiugai),
]
 
#index.html
  
 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>[1]資料庫新增操作</h3>
<a href="/addBook/">新增</a>

<h3>[2]資料庫查詢操作</h3>
<a href="/chaxun/">查詢</a>

<h3>[3]資料庫條件查詢操作</h3>
<a href="/NOchaxun/?id=4">條件查詢</a>

<h3>[4]資料庫條件刪除操作</h3>
<a href="/deletedemo/1/">條件刪除</a>

<h3>[5]資料庫條件修改操作</h3>
<a href="/xiugai/8/中軟卓越/">條件修改</a>
</body>
</html>

#insert.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/addBook/">
{% csrf_token %}
<input type="text" name="bname">
<button type="submit">新增</button>
</form>
</body>
</html>

#chaxun.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>chaxun</title>
</head>
<body>
{% for foo in chaxunkey %}
{{ foo.bid }}-{{ foo.bname }}<br/>
{% endfor %}
<br/>
<table border="1">
<tr>
<td>編號</td>
<td>名稱</td>
</tr>
{% for foo in chaxunkey %}
<tr>
<td>{{ foo.bid }}</td>
<td>{{ foo.bname }}</td>
</tr>
{% empty %}
<tr>
<td colspan="2">暫無資料</td>
</tr>
{% endfor %}

</table>
</body>
</html>

#NOchaxun.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>按編號查詢</title>
</head>
<body>
<h2>{{ NOchaxunkey }}</h2>
資料1:{{ NOchaxunkey.bid }}<br/>
資料2:{{ NOchaxunkey.bname }}
</body>
</html>