I.书籍管理系统
models
class Book(models.Model): """ 书籍 """ title=models.CharField(max_length=32) price=models.IntegerField() dates=models.DateField(null=True) p=models.ForeignKey('Publish') class Publish(models.Model): """ 出版社 """ name=models.CharField(max_length=32) address=models.CharField(max_length=32) city=models.CharField(max_length=32,null=True) class Author(models.Model): """ 作者 """ name=models.CharField(max_length=32) class Book2Author(models.Model): """ 书籍和作者关系表 """ book=models.ForeignKey('Book') author=models.ForeignKey('Author')
urls.py
#---书籍 url(r'^book/',views.book), url(r'^add_book/',views.add_book), url(r'^del_book/',views.del_book), url(r'^edit_book/(d+)/',views.edit_book), #---出版社 url(r'^publish/',views.publish), url(r'^add_publish/',views.add_publish), url(r'^del_publish/',views.del_publish), url(r'^edit_publish/(d+)/',views.edit_publish), #---作者和书籍关系 url(r'^author/', views.author), url(r'^add_author', views.add_author), url(r'^del_author/', views.del_author), url(r'^edit_author/(d+)/', views.edit_author),
views.py
##-------------书籍---------- def book(request): book_list=models.Book.objects.all() for obj in book_list: print(obj.title,obj.p.name) return render(request,'book.html',locals()) def add_book(request): if request.method=='GET': publish_list=models.Publish.objects.all() print(publish_list) return render(request,'add_book.html',locals()) else: title=request.POST.get('title') price=request.POST.get('price') dates=request.POST.get('dates') p_id=request.POST.get('p_id') models.Book.objects.create(title=title,price=price,dates=dates,p_id=p_id) return redirect('/book/') def del_book(request): nid=request.GET.get('nid') models.Book.objects.filter(id=nid).delete() return redirect('/book/') def edit_book(request,a1): if request.method=='GET': current_book_info=models.Book.objects.filter(id=a1).first() publish_list = models.Publish.objects.all() print(locals()) return render(request,'edit_book.html',locals()) else: title=request.POST.get('title') price=request.POST.get('price') dates=request.POST.get('dates') p_id=request.POST.get('p_id') print(title,price,dates,p_id) models.Book.objects.filter(id=a1).update(title=title,price=price,dates=dates,p_id=p_id) return redirect('/book/') #--------------- 出版社 ------------- def publish(request): publish_list=models.Publish.objects.all() return render(request,'publish.html',locals()) def add_publish(request): if request.method=='GET': return render(request,'add_publish.html') else: name=request.POST.get('name') address=request.POST.get('address') city=request.POST.get('city') models.Publish.objects.create(name=name,address=address,city=city) return redirect('/publish/') def del_publish(request): nid=request.GET.get('nid') models.Publish.objects.filter(id=nid).delete() return redirect('/publish/') def edit_publish(request,a1): if request.method=='GET': current_publish_info=models.Publish.objects.filter(id=a1).first() return render(request,'edit_publish.html',locals()) else: name=request.POST.get('name') address=request.POST.get('address') city=request.POST.get('city') models.Publish.objects.filter(id=a1).update(name=name,address=address,city=city) return redirect('/publish/') #------------作者和书籍(多对多)------------ def author(request): author_list=models.Author.objects.all().values('id','name','book2author__book__title') # for i in author_list: # print(i['id'],i['name'],i['book2author__book__title']) # print('******',author_list) result={} for row in author_list: id=row['id'] print('###',id) if row['id'] in result: result[row['id']]['title'].append(row['book2author__book__title']) else: result[row['id']] = {'id':row['id'],'name':row['name'],'title':[row['book2author__book__title']]} print('*********',result) return render(request,'author.html',{'author_list':result.values()}) def add_author(request): if request.method=='GET': book_list=models.Book.objects.all() return render(request,'add_author.html',locals()) else: name=request.POST.get('name') author_id=models.Author.objects.create(name=name) book_ids=request.POST.getlist('book_ids') ###传过来的书籍可能会多个:getlist # print('***#####**',author_id) # print('***#####***',book_ids) #多次连接,多次提交 for b_id in book_ids: models.Book2Author.objects.create(author_id=author_id.id,book_id=b_id) return redirect('/author/') def edit_author(request,a1): if request.method=='GET': author_info=models.Author.objects.filter(id=a1).first() book_id_list=models.Book2Author.objects.filter(author_id=a1).values_list("book_id") book_id_list = [i[0] for i in book_id_list] book_list=models.Book.objects.all() # print('****',author_info) # print('****', book_id_list) return render(request,'edit_author.html',locals()) else: name=request.POST.get('name') book_ids=request.POST.getlist('b_ids') print('b)ids') #更新作者表 models.Author.objects.filter(id=a1).update(name=name) #更新作者和书籍表 #先把当前作者和书籍表 对象关系删除 models.Book2Author.objects.filter(author_id=a1).delete() for id in book_ids: models.Book2Author.objects.create(author_id=a1,book_id=id) return redirect('/author/')
***.html
#-----------书籍表
book.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/bootstrap-3.3.5-dist/css/bootstrap.css"> <style> .container{ margin-top: 200px; margin-left: 150px; } body{ background-image:url("/static/img/6.jpg"); background-size: 100%; background-repeat: no-repeat; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-2"> <a href="/add_book/"><button class="btn btn-primary">添加</button></a> <table class="table table-bordered "> <tr> <th>ID</th> <th>书名</th> <th>价格</th> <th>出版日期</th> <th>出版社</th> <th>操作</th> </tr> {% for obj in book_list %} <tr> <td>{{ obj.id }}</td> <td>{{ obj.title }}</td> <td>{{ obj.price }}</td> <td>{{ obj.dates|date:'Y-m-d' }}</td> <td>{{ obj.p.name }}</td> <td> <a href="/edit_book/{{ obj.id }}" class="btn btn-info">编辑</a> <a href="/del_book/?nid={{ obj.id }} " class="btn btn-warning">删除</a> </td> </tr> {% endfor %} </table> </div> </div> </div>
add_book.html
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加书籍</title> <link rel="stylesheet" href="{% static 'bootstrap-3.3.5-dist/css/bootstrap.css' %}"> <link rel="stylesheet" href="{% static 'bootstrap-3.3.5-dist/js/bootstrap.js' %}"> <style> .container{ margin-top: 100px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-2"> <form action="/add_book/" method="post"> <div class="form-group"> <label for="title">书名</label> <input type="text" class="form-control" id="title" placeholder="title" name="title"> </div> <div class="form-group"> <label for="price">价格</label> <input type="text" class="form-control" id="price" placeholder="price" name="price"> </div> <div class="form-group"> <label for="dates">出版日期</label> <input type="date" class="form-control" id="dates" placeholder="dates" name="dates"> </div> <div class="form-group"> <label for="publish">出版社</label> {# <input type="text" class="form-control" id="publish" placeholder="publish" name="publish">#} <p> <select name="p_id" > {% for row in publish_list %} <option value="{{ row.id }}">{{ row.name }}</option> {% endfor %} </select> </p> </div> <input type="submit" class="btn btn-info"> </form> </div> </div> </div> </body> </html>
edit_book.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>编辑书籍</title> </head> <body> <h1>编辑书籍</h1> <form method="POST" action="/edit_book/{{ a1 }}/"> <p>书籍名称<input type="text" name="title" value="{{ current_book_info.title }}" /></p> <p>价格<input type="text" name="price" value="{{ current_book_info.price }}" /></p> <p>日期<input type="date" name="dates" value="{{ current_book_info.dates|date:'Y-m-d' }}" /></p> <p> 所属出版社 <select name="p_id"> <!-- 循环所有的出版社 --> {% for row in publish_list %} <!-- 如果是当前书籍所在的出版社,则默认选中 --> {% if current_book_info.p_id == row.id %} <option selected value="{{ row.id }}">{{ row.name }}</option> {% else %} <option value="{{ row.id }}">{{ row.name }}</option> {% endif %} {% endfor %} </select> </p> <input type="submit" value="提交" /> </form> </body> </html>
#--------出版社表
publish.html
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'bootstrap-3.3.5-dist/css/bootstrap.css'%}"> <style> .container{ margin-top: 200px; margin-left: 150px; } body{ background-image:url("/static/img/6.jpg"); background-size: 100%; background-repeat: no-repeat; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-2"> <a href="/add_publish/"><button class="btn btn-primary">添加</button></a> <table class="table table-bordered "> <tr> <th>ID</th> <th>出版社</th> <th>地址</th> <th>城市</th> <th>操作</th> </tr> {% for obj in publish_list %} <tr> <td>{{ obj.id }}</td> <td>{{ obj.name }}</td> <td>{{ obj.address }}</td> <td>{{ obj.city }}</td> <td > <a href="/edit_publish/{{ obj.id }}" class="btn btn-info col-md-offset-2">编辑</a> <a href="/del_publish/?nid={{ obj.id }}" class="btn btn-warning col-md-offset-1">删除</a> </td> </tr> {% endfor %} </table> </div> </div> </div> </body> </html>
add_publish.html
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加出版社</title> <link rel="stylesheet" href="{% static 'bootstrap-3.3.5-dist/css/bootstrap.css' %}"> <link rel="stylesheet" href="{% static 'bootstrap-3.3.5-dist/js/bootstrap.js' %}"> <style> .container{ margin-top: 100px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-2"> <form action="/add_publish/" method="POST"> <div class="form-group"> <label for="name">出版社</label> <input type="text" class="form-control" id="name" placeholder="name" name="name"> </div> <div class="form-group"> <label for="address">地址</label> <input type="text" class="form-control" id="address" placeholder="address" name="address"> </div> <div class="form-group"> <label for="city">所在城市</label> <input type="text" class="form-control" id="city" placeholder="city" name="dcity"> </div> <input type="submit" class="btn btn-info"> </form> </div> </div> </div> </body> </html>
edit_author.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>编辑作者</title> </head> <body> <h1>编辑作者</h1> <form method="POST" action="/edit_author/{{ a1 }}/"> <p>作者姓名<input type="text" name="name" value="{{ author_info.name }}" /></p> <div>所写书籍 <p> <select name="b_ids" multiple size="3"> {% for item in book_list %} {% if item.id in book_id_list %} <option value="{{ item.id }}" selected>{{ item.title }}</option> {% else %} <option value="{{ item.id }}">{{ item.title }}</option> {% endif %} {% endfor %} </select> </p> </div> <input type="submit" value="提交" /> </form> </body> </html>
#--------作者表(包含Book和Author 关系)
authot.html
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'bootstrap-3.3.5-dist/css/bootstrap.css'%}"> <style> .container{ margin-top: 200px; margin-left: 150px; } body{ background-image:url("/static/img/6.jpg"); background-size: 100%; background-repeat: no-repeat; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-7 col-md-offset-2"> <a href="/add_author/"><button class="btn btn-primary">添加</button></a> <table class="table table-bordered "> <tr> <th>ID</th> <th>作者姓名</th> <th class="col-md-3">所写书籍</th> <th>操作</th> </tr> {% for obj in author_list %} <tr> <td>{{ obj.id }}</td> <td>{{ obj.name }}</td> <td> {% for item in obj.title %} <span style="display: inline-block;padding: 5px;border: 2px solid #c0a16b">{{ item }}</span> {% endfor %} </td> <td > <a href="/edit_author/{{ obj.id }}" class="btn btn-info col-md-offset-3">编辑</a> <a href="/del_author/?nid={{ obj.id }}" class="btn btn-warning ">删除</a> </td> </tr> {% endfor %} </table> </div> </div> </div>
add_author.html
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加作者</title> <link rel="stylesheet" href="{% static 'bootstrap-3.3.5-dist/css/bootstrap.css' %}"> <link rel="stylesheet" href="{% static 'bootstrap-3.3.5-dist/js/bootstrap.js' %}"> <style> .container{ margin-top: 100px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-2"> <form action="/add_author/" method="post"> <div class="form-group"> <label for="name">作者姓名</label> <input type="text" class="form-control" id="name" placeholder="姓名" name="name"> </div> <div class="form-group"> <label for="title">所写书籍</label> {# <input type="text" class="form-control" id="title" placeholder="所写书籍" name="title">#} <p> <select name="book_ids" multiple size="3"> {% for item in book_list %} <option value="{{ item.id }}">{{ item.title }}</option> {% endfor %} </select> </p> </div> <input type="submit" class="btn btn-info"> </form> </div> </div> </div> </body> </html>
edit_author.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>编辑作者</title> </head> <body> <h1>编辑作者</h1> <form method="POST" action="/edit_author/{{ a1 }}/"> <p>作者姓名<input type="text" name="name" value="{{ author_info.name }}" /></p> <div>所写书籍 <p> <select name="b_ids" multiple size="3"> {% for item in book_list %} {% if item.id in book_id_list %} <option value="{{ item.id }}" selected>{{ item.title }}</option> {% else %} <option value="{{ item.id }}">{{ item.title }}</option> {% endif %} {% endfor %} </select> </p> </div> <input type="submit" value="提交" /> </form> </body> </html>
所用背景图片: