一對一 出版社信息的增刪改查css
一對多 出版社信息與書籍信息的增刪改查html
多對多 書籍信息與做者信息的增刪改查數據庫
from django.db import models class Publisher(models.Model): pid = models.AutoField(primary_key=True) # 自增字段且設置爲主鍵 name = models.CharField(max_length=32,unique=True) # 設置惟一 def __str__(self): return "{}--{}".format(self.pid,self.name) class Book(models.Model): name = models.CharField(max_length=32,unique=True) pub = models.ForeignKey('Publisher',on_delete=models.CASCADE) # 外鍵 要連接的類名能夠直接寫,也能夠寫在字符串內 級聯刪除 class Author(models.Model): name = models.CharField(max_length=32) books = models.ManyToManyField('Book') # 多對多,生成第三張表
1.設計URLnpm
URLdjango
2.寫函數bootstrap
from app01 import models def publisher_list(request): '''展現數據''' # 獲取數據庫內全部出版社的數據 publishers = models.Publisher.objects.all().order_by('pid') # 獲取對象列表並按照pid字段排序 return render(request,'publisher_list.html',{'publishers':publishers}) # 第三個參數是爲了給html文件傳值調用 def add_publisher(request): '''新增數據''' if request.method == 'POST': # 獲取提交的數據(括號內爲input的name屬性值),沒有默認空字符串 new_name = request.POST.get('new_name','').strip() # 設定輸入不能爲空 if not new_name: return render(request,'add_publisher.html',{'err_msg':'輸入不能爲空','name':new_name}) # 設定不能與數據庫現有數據重複 obj_list = models.Publisher.objects.filter(name=new_name) # 在數據庫中查詢數據是否存在 if obj_list: # 數據重複 return render(request, 'add_publisher.html', {'err_msg': '出版社名稱已存在', 'name': new_name}) # orm往數據庫中寫入數據 if new_name and not obj_list: models.Publisher.objects.create(name=new_name) return redirect('/publisher_list/') # 若是不是post請求,仍是返回本頁面 return render(request,'add_publisher.html') def delete_publisher(request): '''刪除數據''' # 找到須要刪除的數據的id pk = request.GET.get('pk') # 經過id在數據庫中找到對應的數據 obj = models.Publisher.objects.filter(pid=pk) # 若是數據不存在(經過地址欄指定id進行刪除) if not obj: return HttpResponse('要編輯的數據不存在') # 刪除數據 返回展現頁面 obj.delete() return redirect('/publisher_list/') def edit_publisher(request): # 找到須要編輯的數據的id pk = request.GET.get('pk') # 經過id在數據庫中找到對應的數據 對象列表<QuerySet [<Publisher: 1--人民郵電出版社>]> obj_list = models.Publisher.objects.filter(pk=pk) # 若是數據不存在(經過地址欄指定id進行編輯) if not obj_list: return HttpResponse('要編輯的數據不存在') # 拿到數據 1--人民郵電出版社 obj = obj_list[0] err_msg = '' if request.method == 'POST': # 獲取用戶輸入的數據 new_name = request.POST.get('new_name','').strip() # 設定輸入不能爲空,返回添加頁面 if not new_name: err_msg = '輸入不能爲空' # return render(request, 'edit_publisher.html', {'obj': obj, 'err_msg': '輸入不能爲空'}) # 設定不能與數據庫現有數據重複 obj_list = models.Publisher.objects.filter(name=new_name) # 在數據庫中查詢數據是否存在 if obj_list: #有重複數據,返回添加頁面 err_msg = '出版社名稱已存在' # return render(request, 'edit_publisher.html', {'obj': obj, 'err_msg': '出版社名稱已存在'}) #修改數據 if new_name and not obj_list: obj.name = new_name # 在內存中修改 obj.save() # 寫入數據庫 return redirect('/publisher_list/') #跳轉到展現頁面 return render(request,'edit_publisher.html',{'obj':obj,'err_msg':err_msg}) return render(request, 'edit_publisher.html')
3.寫模板app
ide
{{ 變量 }},能夠使用函數傳的參數進行渲染<html lang="en"> <head> <meta charset="UTF-8"> <title>出版社信息</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container"> <a class="btn btn-success" href="/add_publisher/" role="button">新增</a> <table class="table table-condensed"> <thead> <tr> <th>序號</th> <th>ID</th> <th>名稱</th> <th>操做</th> </tr> </thead> <tbody> {% for foo in publishers %} # 開始循環 <tr> <td>{{ forloop.counter }}</td> <td>{{ foo.pk }}</td> <td>{{ foo.name }}</td> <td> <a class="btn btn-info" href="/delete_publisher/?pk={{ foo.pk }}" role="button">刪除</a> <a class="btn btn-info" href="/edit_publisher/?pk={{ foo.pk }}" role="button">編輯</a> </td> </tr> {% endfor %} # 結束循環 </tbody> </table> </div> </body> </html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>添加信息</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container"> <form class="form-horizontal" method="post" action=""> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">名稱</label> <div class="col-sm-5"> <input type="text" class="form-control" name="new_name" placeholder="出版社名稱" value="{{ name }}"> <span>{{ err_msg }}</span> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>編輯信息</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container"> <form class="form-horizontal" method="post" action=""> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">名稱</label> <div class="col-sm-5"> <input type="text" class="form-control" name="new_name" value="{{ obj.name}}"> <span>{{ err_msg }}</span> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form> </div> </body> </html>
1.設計URL函數
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^publisher_list/', views.publisher_list),
url(r'^add_publisher/', views.add_publisher),
url(r'^delete_publisher/', views.delete_publisher),
url(r'^edit_publisher/', views.edit_publisher),
url(r'^book_list/', views.book_list),
url(r'^add_book/', views.add_book),
url(r'^del_book/', views.del_book),
url(r'^edit_book/', views.edit_book),
]
2.寫函數oop
def book_list(request): '''書籍數據展現''' # 將數據庫中的數據讀取出來 books = models.Book.objects.all() #對象列表 return render(request,'book_list.html',{'books':books}) def add_book(request): '''添加書籍信息''' # 獲取全部出版社信息 publishers = models.Publisher.objects.all() if request.method == 'POST': # 獲取提交的數據 new_name = request.POST.get('new_name','').strip() pub_id = request.POST.get('pub_id') err_msg = '' # 不能爲空 if not new_name: err_msg = '輸入不能爲空' # 不能與現有書籍重複 obj = models.Book.objects.filter(name=new_name) if obj: err_msg = '書籍已存在' # 寫入數據庫 if new_name and not obj: models.Book.objects.create(name=new_name,pub_id=pub_id) # 跳轉到展現頁面 return redirect('/book_list/') return render(request,'add_book.html',{'publishers':publishers,'err_msg':err_msg,'new_name':new_name}) # 若是不是post請求 return render(request,'add_book.html',{'publishers':publishers}) def del_book(request): '''刪除書籍''' # 獲取刪除數據的pk pk = request.GET.get('pk') # 根據pk在數據庫中找出對應的數據 obj = models.Book.objects.filter(pk=pk) # 若是pk不存在 if not obj: return HttpResponse('要刪除的數據不存在') # 刪除數據 obj.delete() # 返回展現頁面 return redirect('/book_list/') def edit_book(request): '''編輯書籍信息''' # 找到須要編輯數據的pk pk = request.GET.get('pk') #根據pk找到數據庫中的對應的數據 obj_list = models.Book.objects.filter(pk=pk) obj = obj_list[0] publishers = models.Publisher.objects.all() if request.method == 'POST': # 獲取提交的數據 new_name = request.POST.get('new_name','').strip() pub_id = request.POST.get('pub_id','').strip() err_msg = '' if not new_name: err_msg = '輸入不能爲空' book_obj = models.Book.objects.filter(name=new_name) if book_obj: err_msg = '書籍已存在' if new_name and not book_obj: obj.name = new_name obj.pub_id = pub_id obj.save() return redirect('/book_list/') return render(request,'edit_book.html',{'err_msg':err_msg,'obj':obj,'publishers':publishers}) return render(request,'edit_book.html',{'publishers':publishers,'obj':obj})
3.寫模板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>書籍信息</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="panel panel-primary" style="margin-top: 50px"> <div class="panel-heading">書籍信息</div> <div class="panel-body"> <a class="btn btn-primary" href="/add_book/" role="button">新增</a> <table class="table table-condensed"> <thead> <tr> <th>序號</th> <th>ID</th> <th>書名</th> <th>出版社</th> <th>操做</th> </tr> </thead> <tbody> {% for book in books %} <tr> <td>{{ forloop.counter }}</td> <td>{{ book.pk }}</td> <td>{{ book.name }}</td> <td>{{ book.pub.name }}</td> <td> <a class="btn btn-danger" href="/del_book/?pk={{ book.pk }}" role="button">刪除</a> <a class="btn btn-success" href="/edit_book/?pk={{ book.pk }}" role="button">編輯</a> </td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加書籍信息</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="panel panel-primary" style="margin-top: 50px"> <div class="panel-heading">添加書籍信息</div> <div class="panel-body"> <form class="form-horizontal" method="post" action=""> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">書名</label> <div class="col-sm-5"> <input type="text" class="form-control" name="new_name" placeholder="書名" value="{{ new_name }}"> <span>{{ err_msg }}</span> </div> </div> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">出版社</label> <div class="col-sm-5"> <select name="pub_id" id="" class="form-control"> {% for publisher in publishers %} <option value="{{ publisher.pk }}">{{ publisher.name }}</option> <span>{{ err_msg }}</span> {% endfor %} </select> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form> </div> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>編輯書籍信息</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="panel panel-primary" style="margin-top: 50px"> <div class="panel-heading">編輯書籍信息</div> <div class="panel-body"> <form class="form-horizontal" method="post" action=""> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">書名</label> <div class="col-sm-5"> <input type="text" class="form-control" name="new_name" value="{{ obj.name }}"> <span>{{ err_msg }}</span> </div> </div> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">出版社</label> <div class="col-sm-5"> <select name="pub_id" id="" class="form-control"> {% for publisher in publishers %} {% if publisher == obj.pub %} <option selected value="{{ publisher.pk }}">{{ publisher.name }}</option> {% else %} <option value="{{ publisher.pk }}">{{ publisher.name }}</option> {% endif %} {% endfor %} </select> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form> </div> </div> </div> </body> </html>
1.設計URL
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^publisher_list/', views.publisher_list),
url(r'^add_publisher/', views.add_publisher),
url(r'^delete_publisher/', views.delete_publisher),
url(r'^edit_publisher/', views.edit_publisher),
url(r'^book_list/', views.book_list),
url(r'^add_book/', views.add_book),
url(r'^del_book/', views.del_book),
url(r'^edit_book/', views.edit_book),
url(r'^author_list/', views.author_list),
url(r'^add_author/', views.add_author),
url(r'^del_author/', views.del_author),
url(r'^edit_author/', views.edit_author),
]
2.寫函數
def author_list(request): '''展現做者''' authors = models.Author.objects.all() # for i in authors: # print(i.books) # 關係管理對象 # print(i.books.all()) # 關聯的書籍對象 return render(request,'author_list.html',{'authors':authors}) def add_author(request): '''新增做者''' if request.method == 'POST': name = request.POST.get('name') book_id = request.POST.getlist('book_id') # get方法只會取一條數據,書籍爲多選,使用getlist方法,結果爲列表 # 寫入數據庫,返回展現頁面 author_obj = models.Author.objects.create(name=name) # 建立做者對象 author_obj.books.set(book_id) # set方法 設置做者和書籍的多對多的關係 每次都是從新設置.刪除原有數據,生成新的id return redirect('/author_list/') books = models.Book.objects.all() return render(request,'add_author.html',{'books':books}) def del_author(request): '''刪除做者''' pk = request.GET.get('pk') models.Author.objects.filter(pk=pk).delete() return redirect('/author_list/') def edit_author(request): '''編輯做者''' pk = request.GET.get('pk') edit_obj = models.Author.objects.get(pk=pk) if request.method == 'POST': name = request.POST.get('name') books_id = request.POST.getlist('book_id') edit_obj.name = name edit_obj.save() edit_obj.books.set(books_id) # 修改做者與書籍的多對多關係 return redirect('/author_list/') books = models.Book.objects.all() return render(request,'edit_author.html',{'obj':edit_obj,'books':books})
3.寫模板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>做者信息</title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7/css/bootstrap.css"> </head> <body> <div class="container"> <div class="panel panel-primary" style="margin-top: 50px"> <div class="panel-heading"><h4>做者信息</h4></div> <div class="panel-body"> <a class="btn btn-primary" href="/add_author/" role="button">新增</a> <table class="table table-condensed"> <thead> <tr> <th>序號</th> <th>ID</th> <th>做者</th> <th>表明做</th> <th>操做</th> </tr> </thead> <tbody> {% for author in authors %} <tr> <td>{{ forloop.counter }}</td> <td>{{ author.pk }}</td> <td>{{ author.name }}</td> <td> {% for book in author.books.all %} {{ book.name }} {% endfor %} </td> <td> <a class="btn btn-danger" href="/del_author/?pk={{ author.pk }}" role="button">刪除</a> <a class="btn btn-success" href="/edit_author/?pk={{ author.pk }}" role="button">編輯</ </td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加做者信息</title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7/css/bootstrap.css"> </head> <body> <div class="container"> <div class="panel panel-primary" style="margin-top: 50px"> <div class="panel-heading">添加做者信息</div> <div class="panel-body"> <form class="form-horizontal" method="post" action=""> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">姓名</label> <div class="col-sm-5"> <input type="text" class="form-control" name="name" placeholder="姓名"> <span>{{ err_msg }}</span> </div> </div> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">表明做</label> <div class="col-sm-5"> <select name="book_id" id="" class="form-control" multiple> {% for book in books %} <option value="{{ book.pk }}">{{ book.name }}</option> {% endfor %} </select> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form> </div> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>修改做者信息</title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7/css/bootstrap.css"> </head> <body> <div class="container"> <div class="panel panel-primary" style="margin-top: 50px"> <div class="panel-heading">編輯做者信息</div> <div class="panel-body"> <form class="form-horizontal" method="post" action=""> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">姓名</label> <div class="col-sm-5"> <input type="text" class="form-control" name="new_name" value="{{ obj.name }}"> </div> </div> <div class="form-group"> <label for="inputName3" class="col-sm-2 control-label">表明做</label> <div class="col-sm-5"> <select name="book_id" id="" class="form-control" multiple> {% for book in books %} {% if book in obj.books.all %} <option selected value="{{ book.pk }}">{{ book.name }}</option> {% else %} <option value="{{ book.pk }}">{{ book.name }}</option> {% endif %} {% endfor %} </select> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form> </div> </div> </div> </body> </html>
建立多對多關係的三種方式
1.Django建立第三張表
class Author(models.Model): name = models.CharField(max_length=32) books = models.ManyToManyField('Book') # 表示多對多的關係 生成第三張表
2.本身建立第三張表
能夠在第三張表中添加另外的字段和數據,利用django直接建立是作不到的,可是查詢數據比較複雜
class Author(models.Model): name = models.CharField(max_length=32) class Author_Book(models.Model): author = models.ForeignKey('Author') book = models.ForeignKey('Book') time = models.CharField(max_length=32)
3.Django + 自建表
經過Django和自建表的形式,既能夠在表中添加字段和數據,也解決了查詢困難的事情
class Author(models.Model): name = models.CharField(max_length=32) books = models.ManyToManyField('Book',through='Author_Book') # 表示多對多的關係 class Author_Book(models.Model): author = models.ForeignKey('Author') book = models.ForeignKey('Book') time = models.CharField(max_length=32)