练习项目图书管理
图书管理初级练习
前端页面
book.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>书籍展示</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"> </head> <body> <h1>查看书籍</h1> <div class="col-xs-8 col-xs-offset-2" style="padding-top: 15px;"> <div> <div class="panel-body"> <div class="row"> <a href="{% url 'book_add' %}"><button type="button" class="btn btn-primary">添加书籍 </button></a> <div class="pull-right"> <form action="{% url 'search' %}" method="post"> {% csrf_token %} <select name="select" id=""> <option value="0" selected>请选择搜索方式</option> <option value="1">出版社</option> <option value="2">价格</option> <option value="3">出版日期</option> <option value="4">书籍名称</option> </select> <input type="text" name="search"> <button type="submit">查询</button> </form> </div> <a href="{% url 'search_1' %}" type="submit" class="btn btn-primary pull-right">查看语句</a>
{# <a class="btn btn-primary" href="{% url 'book_add' %}" type="button"> 添加书籍</a>#}
</div>
</div>
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>编号</th>
<th>书籍名称</th>
<th>价格</th>
<th>出版日期</th>
<th>出版社</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for foo in ret %}
<tr class="active">
<td>{{ forloop.counter }}</td>
<td>{{ foo.title }}</td>
<td>{{ foo.price }}</td>
<td>{{ foo.publication_date|date:'Y-m-d' }}</td>
<td>{{ foo.press }}</td>
<td>
<a type="button" class="btn btn-default btn btn-warning btn-lg" style="font-size: 10px;" href="{% url 'book_edit' foo.id %}">编辑</a>
<a type="submit" class="btn btn-default btn-lg btn btn-danger" style="font-size: 10px;" href="{% url 'book_del' foo.id %}">删除</a>
</td>
</tr>{% endfor %} </tbody>
</table>
</div>
</div>
</div>
</body>
</html>
book_add.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js">
</head>
<body>
<div class=" row" >
<div class="col-md-8 col-md-offset-2" style="text-align: center">
<h1>图书添加<small>----郭建凯亲历打造</small></h1>
</div>
<div class="alert alert-success" role="alert" style="font-size:50px;text-align: center">添加书籍</div>
<div class="clo-xs-8 col-md-offset-2">
<form action="" method="post">
{% csrf_token %}
<div class="form-group center-block" style="font-size: 50px;display: block;">
<label for="title">书籍名称</label>
<input type="text" class="form-control" id="title" placeholder="书籍名称" style="height: 100px" name="title">
</div>
<div class="form-group center-block" style="font-size: 50px;display: block;">
<label for="price">价格</label>
<input type="number" class="form-control" id="price" placeholder="价格" style="height: 100px" name="price">
</div>
<div class="form-group center-block" style="font-size: 50px;display: block;">
<label for="publication_date">出版日期</label>
<input type="date" class="form-control" id="publication_date" placeholder="出版日期" style="height: 100px;" name="publication_date">
</div>
<div class="form-group center-block" style="font-size: 50px;display: block;">
<label for="press">出版社</label>
<input type="text" class="form-control" id="press" placeholder="出版社" style="height: 100px" name="press">
</div>
<a class="btn btn-success btn-lg pull-right" href="{% url 'book' %}" role="button" style="font-size: 100px"><button type="submit">Submit</button></a>
</form>
</div>
</div>
</body>
</html>
book_edit.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js">
</head>
<body>
<div class="alert alert-success" role="alert" style="font-size:50px;text-align: center">编辑书籍</div>
<div class="clo-xs-8 col-md-offset-2">
<form action="" method="post">
{% csrf_token %}
<div class="form-group center-block" style="font-size: 50px;display: block;">
<label for="title">书籍名称</label>
<input type="text" class="form-control" id="title" placeholder="书籍名称" style="height: 100px" name="title" value="{{ ret.title }}">
</div>
<div class="form-group center-block" style="font-size: 50px;display: block;">
<label for="price">价格</label>
<input type="number" class="form-control" id="price" placeholder="价格" style="height: 100px" name="price" value="{{ ret.price }}">
</div>
<div class="form-group center-block" style="font-size: 50px;display: block;">
<label for="publication_date">出版日期</label>
<input type="date" class="form-control" id="publication_date" placeholder="出版日期" style="height: 100px;" name="publication_date" value="{{ ret.publication_date|date:'Y-m-d' }}">
</div>
<div class="form-group center-block" style="font-size: 50px;display: block;">
<label for="press">出版社</label>
<input type="text" class="form-control" id="press" placeholder="出版社" style="height: 100px" name="press" value="{{ ret.press }}">
</div>
<a class="btn btn-success btn-lg pull-right" href="{% url 'book' %}" role="button" style="font-size: 100px"><button type="submit">Submit</button></a>
</form>
</div>
</body>
</html>
app01的models
from django.db import models
Create your models here.
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.FileField()
publication_date = models.DateField()
press = models.CharField(max_length=32)
项目文件中的url
from django.conf.urls import url from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^book/$', views.book,name='book'),
url(r'^book/add/$', views.book_add,name='book_add'),
url(r'^book/edit/(\d+)/$', views.book_edit,name='book_edit'),
url(r'^book/del/(\d+)/$', views.book_del,name='book_del'),
url(r'^search/1/$', views.search_1,name='search_1'),
url(r'^search/$', views.search,name='search'),
]
app01的view
from django.shortcuts import render,redirect,HttpResponse from django.views import View from app01 import models from django.urls import reverse
Create your views here.
def book(request):
ret = models.Book.objects.all()
return render(request, 'book.html',{'ret': ret})
def book_add(request):
print(456)if request.method == 'GET': # return HttpResponse("ok") return render(request,'book_add.html') else: data = request.POST.dict() del data['csrfmiddlewaretoken'] a = models.Book(**data) a.save() return redirect('book')
def book_edit(request,book_id):
if request.method == 'GET':
ret = models.Book.objects.filter(id=book_id)[0]
return render(request,'book_edit.html',{'ret':ret})
else:
data = request.POST.dict()
del data['csrfmiddlewaretoken']
models.Book.objects.filter(id=book_id).update(**data)
return redirect('book')def book_del(request,book_id):
models.Book.objects.filter(id=book_id).delete()
return redirect('book')def search_1(request):
# 1
# 查询某某出版社出版过的价格大于200的书籍
# ret = models.Book.objects.filter(price__gt=200).values('title')
# 2
# 查询2017年8月出版的所有以py开头的书籍名称
# ret = models.Book.objects.filter(publication_date__lt="2017-08-01",title__istartswith='py').values('title')
# 3
# 查询价格为50, 100
# 或者150的所有书籍名称及其出版社名称
# ret = models.Book.objects.filter(price__in=[50,100,150]).values('title','press')
# 4
# 查询价格在100到200之间的所有书籍名称及其价格
# ret = models.Book.objects.filter(price__in=range(100,200)).values('title','price')
# 5
# 查询所有人民出版社出版的书籍的价格(从高到低排序,去重)
ret = models.Book.objects.all().order_by('price',).values('price').distinct()print(ret) return HttpResponse('ok')
def search(request):
data1 = request.POST.get('select')
ret = request.POST.get('search')
if data1 == '0':
ret = models.Book.objects.all()
return render(request, 'book.html',{'ret': ret})
elif data1 == '1':
ret = models.Book.objects.filter(press=ret)
return render(request,'book.html',{'ret':ret})
elif int(data1) == 2:
ret = models.Book.objects.filter(price=ret)
print(ret)
return render(request,'book.html',{'ret':ret})
elif data1 == '3':
try:
ret = models.Book.objects.filter(publication_date__year=ret)
return render(request,'book.html',{'ret':ret})
except ValueError:
return render(request,'book.html')
elif data1 == '4':
ret = models.Book.objects.filter(title=ret)
return render(request,'book.html',{'ret':ret})