需求
实现功能:
1.添加数据--书籍名称,价格,出版日期,出版社,作者。(出版社和作者为可选)
2.查看书籍--编辑,删除
2.1点击添加书籍,跳转到添加书籍页面
2.2编辑时要保留之前的原始数据
URL配置
from django.conf.urls import url,include
from django.contrib import admin
from app03 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
# 添加书籍
url(r'^add_book/',views.add_book,name='add_book'),
# 展示书籍
url(r'^show_book/',views.show_book,name='show_book'),
# 编辑书籍
url(r'^edit_book/(\d+)/',views.edit_book,name='edit_book'),
# 删除书籍
url(r'^delete_book/(\d+)/',views.delete_book,name='delete_book'),
]
一.添加书籍
views.py:
# 添加数据
def add_book(request):
if request.method == 'GET':
publish_obj = models.Publish.objects.all()
authors_obj = models.Author.objects.all()
return render(request, 'manytable/add_book.html', {'publish_obj': publish_obj, 'authors_obj': authors_obj})
else:
authors = request.POST.getlist('authors')
all_data = request.POST.dict()
del all_data['csrfmiddlewaretoken']
del all_data['authors']
book_obj = models.Book.objects.create(**all_data)
book_obj.authors.add(*authors)
return redirect('app03:show_book')
html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>添加书籍</h1>
<form action="" method="post">
{% csrf_token %}
<div>
书籍名称:<input type="text" name="title">
</div>
<div>
价格:<input type="text" name="price">
</div>
<div>
出版日期:<input type="date" name="publishDate">
</div>
<div>
出版社:
<select name="publishs_id" id="publishs">
{% for publish in publish_obj %}
<option value="{{ publish.nid }}">{{ publish.name }}</option>
{% endfor %}
</select>
</div>
<div>
作者:
<select name="authors" id="authors" multiple>
{% for author in authors_obj %}
<option value="{{ author.nid }}">{{ author.name }}</option>
{% endfor %}
</select>
</div>
<div>
<input type="submit">
</div>
</form>
</body>
</html>
二.展示书籍
views.py:
# 展示书籍
def show_book(request):
book_data = models.Book.objects.all()
return render(request, 'manytable/show_book.html', {'book_data': book_data})
html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>
<a href="{% url 'app03:add_book' %}">添加书籍</a>
</h1>
<table border="1" cellpadding="10">
<thead>
<tr>
<th>编号</th>
<th>书籍名称</th>
<th>价格</th>
<th>出版日期</th>
<th>出版社</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book_obj in book_data %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.price }}</td>
<td>{{ book_obj.publishDate|date:'Y-m-d' }}</td>
<td>{{ book_obj.publishs.name }}</td>
<td>{{ book_obj.get_user_name }}</td>
<td>
<a href="{% url 'app03:edit_book' book_obj.nid %}">编辑</a>
<a href="{% url 'app03:delete_book' book_obj.nid %}">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
三.编辑书籍
views.py:
# 编辑书籍
def edit_book(request, book_id):
book_obj = models.Book.objects.filter(pk=book_id)
if request.method == 'GET':
book_obj = book_obj.first()
publish_obj = models.Publish.objects.all()
authors_obj = models.Author.objects.all()
return render(request, 'manytable/edit_book.html',
{'book_obj': book_obj, 'publish_obj': publish_obj, 'authors_obj': authors_obj})
else:
authors = request.POST.getlist('authors')
all_data = request.POST.dict()
del all_data['csrfmiddlewaretoken']
del all_data['authors']
book_obj.update(**all_data)
book_obj.first().authors.set(authors)
return redirect('app03:show_book')
html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>编辑书籍</h1>
<form action="" method="post">
{% csrf_token %}
<div>
书籍名称:<input type="text" name="title" value="{{ book_obj.title }}">
</div>
<div>
价格:<input type="text" name="price" value="{{ book_obj.price }}">
</div>
<div>
出版日期:<input type="date" name="publishDate" value="{{ book_obj.publishDate|date:'Y-m-d' }}">
</div>
<div>
出版社:
<select name="publishs_id" id="publishs">
{% for publish in publish_obj %}
{% if publish.nid == book_obj.publishs.nid %}
<option value="{{ publish.nid }}" selected>{{ publish.name }}</option>
{% else %}
<option value="{{ publish.nid }}">{{ publish.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div>
作者:
<select name="authors" id="authors" multiple>
{% for author in authors_obj %}
{% if author in book_obj.authors.all %}
<option value="{{ author.nid }}" selected>{{ author.name }}</option>
{% else %}
<option value="{{ author.nid }}">{{ author.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div>
<input type="submit">
</div>
</form>
</body>
</html>
四.删除书籍
views.py:
def delete_book(request, book_id):
models.Book.objects.filter(pk=book_id).delete()
return redirect('app03:show_book')