一.需求:
实现功能:
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>
二.外部文件使用models
import os
if __name__ == '__main__':
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "books.settings") #使用django环境
import django
django.setup() #执行django环境
from app01 import models
data_list = []
for i in range(1,10):
obj = models.Student(
name= '葵花宝典第%s式' %i,
age = 20 + i,
date ='198%s-12-12' % i,
)
data_list.append(obj)
models.Student.objects.bulk_create(data_list) #批量创建
三.查询题练习
1查询某某出版社出版过的价格大于200的书籍
2查询2017年8月出版的所有以py开头的书籍名称
3查询价格为50, 100或者150的所有书籍名称及其出版社名称
4查询价格在100到200之间的所有书籍名称及其价格
5查询所有人民出版社出版的书籍的价格(从高到低排序,去重)