图书管理系统(二):图书列表的增加、删除和编辑
-
步骤一:创建图书列表
-
步骤二:增加图书
-
步骤三:删除图书
-
步骤四:编辑图书
步骤一:创建图书列表
1.创建数据库Book:
#app_publisher/models.py
class Book(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=64, null=False, unique=True)
publisher = models.ForeignKey(to='publisher')
#由于ForeignKey会自动把publisher属性后面加_id,也就是数据库中是publisher_id,因此在这里把_id给去掉了
python3 manage.py makemigrations #用小本本将models.py里面的改动记录下来,记录到app1/migrations文件夹下面
python3 manage.py migrate #把改动翻译成SQL语句去数据库执行
2.写入函数:
url(r'^book_list/', views.book_list),
def book_list(request):
data = models.Book.objects.all()
return render(request, 'book_list.html', {'data': data})
3.写book_list.html文件:
<!DOCTYPE html>
<html>
<head>
<title>图书列表</title>
</head>
<body>
<table border='1'>
<tr>
<th>序号</th>
<th>id</th>
<th>title</th>
<th>publisher</th>
</tr>
{% for i in data %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ i.id }}</td>
<td>{{ i.title }}</td>
<td>{{ i.publisher.name }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
步骤二:增加图书
1.写入函数:
url(r'^add_book/', views.add_book),
def add_book(request):
if request.method == 'POST':
add_name = request.POST.get('add_name')
new_publisher = request.POST.get('publisher')
models.Book.objects.create(title=add_name, publisher_id=new_publisher).save()
return redirect('/book_list/')
data = models.publisher.objects.all()
return render(request, 'add_book.html', {'data': data})
2.写html文件:
<a href="/add_book/">添加新图书</a>
<!DOCTYPE html>
<html>
<head>
<title>增加图书</title>
</head>
<body>
<h1>增加图书</h1>
<form action="/add_book/" method="post">
<input type="text" name="add_name">
出版社:
<select name='publisher'>
{% for i in data %}
<option value="{{ i.id }}">{{ i.name }}</option>
{% endfor %}
</select>
<input type="submit" value="提交">
</form>
</body>
</html>
步骤三:删除图书
1.写入函数:
url(r'^delete_book/', views.delete_book),
def delete_book(request):
ret = request.GET.get('id')
models.Book.objects.get(id=ret).delete()
return redirect('/book_list/')
2.在book_list.html中增加删除功能:
{% for i in data %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ i.id }}</td>
<td>{{ i.title }}</td>
<td>{{ i.publisher.name }}</td>
<td><a href="/delete_book/?id={{ i.id }}">删除</a></td>
</tr>
{% endfor %}
步骤四:编辑图书
1.写入函数:
url(r'^edit_book/', views.edit_book),
def edit_book(request):
if request.method == 'POST':
edit_id = request.POST.get('edit_id')
edit_name = request.POST.get('edit_title')
print(edit_name)
edit_publisher = request.POST.get('edit_publisher')
ret = models.Book.objects.get(id=edit_id)
ret.title = edit_name
ret.publisher_id = edit_publisher
ret.save()
return redirect('/book_list/')
edit_id = request.GET.get('id')
ret = models.Book.objects.get(id=edit_id)
datas = models.publisher.objects.all()
return render(request, 'edit_book.html', {'datas': datas, 'ret':ret})
2.写html文件:
<td><a href="/edit_book/?id={{ i.id }}">编辑</a></td>
<!DOCTYPE html>
<html>
<head>
<title>修改图书</title>
</head>
<body>
<h1>修改图书</h1>
<form action="/edit_book/" method="post">
<input type="text" name="edit_id" value="{{ ret.id }}" style="display: none">
<input type="text" name='edit_title' value="{{ret.title}}">
<select name="edit_publisher">
{% for data in datas %}
{% if ret.publisher_id == data.id %}
<option selected value="{{ data.id }}">{{ data.name }}</option>
{% else %}
<option value="{{ data.id }}">{{ data.name }}</option>
{% endif %}
{% endfor %}
</select>
<input type="submit" value="提交">
</form>
</body>
</html>