需求:
實現功能:
1.添加數據--書籍名稱,價格,出版日期,出版社
2.查看書籍--編輯,刪除
2.1點擊添加書籍,跳轉到添加書籍頁面
2.2編輯時要保留之前的原始數據
創建Book表:
class Book(models.Model):
name = models.CharField(max_length=16)
price = models.IntegerField()
publish_date = models.DateField()
publish = models.CharField(max_length=16)
urls.py文件中:
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{1})/',views.edit_book,name='edit_book'),
url(r'^delete/(\d{1})/',views.delete,name='delete'),
views.py文件中:
from app01 import models
#添加書籍
def add_book(request):
if request.method == 'GET':
return render(request,'orm/add_book.html')
else:
models.Book.objects.create(
name=request.POST.get('name'),
price=request.POST.get('price'),
publish_date=request.POST.get('publish_date'),
publish=request.POST.get('publish'),
)
return redirect('app01:show_book')
#展示書籍
def show_book(request):
all_data = models.Book.objects.all()
return render(request,'orm/show_book.html',{'all_data':all_data})
#編輯書籍
def edit_book(request,num):
if request.method == 'GET':
edit_data = models.Book.objects.get(id=num)
return render(request,'orm/edit_book.html',{'edit_data':edit_data})
else:
models.Book.objects.filter(id=num).update(
name=request.POST.get('name'),
price=request.POST.get('price'),
publish_date=request.POST.get('publish_date'),
publish=request.POST.get('publish'),
)
return redirect('app01:show_book')
#刪除
def delete(request,num):
models.Book.objects.filter(id=num).delete()
return redirect('app01:show_book')
添加書籍頁:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
<div>
書籍名稱:<input type="text" name="name">
</div>
<div>
價格:<input type="text" name="price">
</div>
<div>
出版日期:<input type="date" name="publish_date">
</div>
<div>
出版社:<input type="text" name="publish">
</div>
<div>
<input type="submit">
</div>
</form>
</body>
</html>
查看書籍頁:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="{% url 'app01:add_book' %}">添加書籍</a>
<table border="1" cellpadding="10" >
<!--標題-->
<thead>
<tr>
<th>編號</th>
<th>書籍名稱</th>
<th>價格</th>
<th>出版日期</th>
<th>出版社</th>
<th>操作</th>
</tr>
</thead>
<!--內容-->
<tbody>
{% for book in all_data %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book.name }}</td>
<td>{{ book.price }}</td>
<td>{{ book.publish_date|date:'Y-m-d' }}</td>
<td>{{ book.publish }}</td>
<td>
<a href="{% url 'app01:edit_book' book.id %}">編輯</a>
<a href="{% url 'app01:delete' book.id %}">刪除</a>
<td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
編輯書籍頁:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
<div>
書籍名稱:<input type="text" name="name" value="{{ edit_data.name }}">
</div>
<div>
價格:<input type="text" name="price" value="{{ edit_data.price }}">
</div>
<div>
出版日期:<input type="date" name="publish_date" value="{{ edit_data.publish_date|date:'Y-m-d' }}">
</div>
<div>
出版社:<input type="text" name="publish" value="{{ edit_data.publish }}">
</div>
<div>
<input type="submit">
</div>
</form>
</body>
</html>