-
步驟一:建立做者列表
-
步驟二:添加做者
-
步驟三:刪除做者
-
步驟四:編輯做者
步驟一:建立做者列表
1.先建立數據表
class Author(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32, null=False, unique=True)
book = models.ManyToManyField(to='Book') #會自動生成第三方表,內容是id,author_id,book_id,而後添加數據
2.寫入函數:
url(r'^author_list/', views.author_list),
def author_list(request):
datas = models.Author.objects.all()
return render(request, 'author_list.html', {'datas': datas})
3.寫html文件:
<!DOCTYPE html>
<html>
<head>
<title>做者列表</title>
</head>
<body>
<h1>做者列表</h1>
<table border="1">
<thead>
<tr>
<th>序號</th>
<th>id</th>
<th>做者</th>
<th>做品</th>
</tr>
</thead>
<tbody>
{% for data in datas %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ data.id }}</td>
<td>{{ data.name }}</td>
<td>
{% for book in data.book.all %}
{% if forloop.last %} #if判斷,若是是倒數第二個,則不加;,不然加
{{ book.title }}
{% else %}
{{ book.title }};
{% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
步驟二:添加做者
1.寫入函數:
url(r'^add_author', views.add_author),
def add_author(request):
if request.method == 'POST':
new_name = request.POST.get('add_name')
new_books = request.POST.
getlist('books')
ret = models.Author.objects.create(name=new_name)
ret.book.
set(new_books)
ret.save()
return redirect('/author_list/')
ret = models.Book.objects.all()
return render(request, 'add_author.html', {'book_list': ret})
2.寫html文件:
<!DOCTYPE html>
<html>
<head>
<title>添加做者</title>
</head>
<body>
<h1>添加做者</h1>
<form action="/add_author/" method="post">
做者:<input type="text" name="add_name">
<select name="books" multiple='
multiple'>
{% for book in book_list %}
<option value="{{ book.id }}">{{ book.title }}</option>
{% endfor %}
</select>
<input type="submit" value="提交">
</form>
</body>
</html>
(遺留了一個問題:表單的多選沒有生效)
步驟三:刪除做者
1.寫入函數:
url(r'^delete_author', views.delete_author),
def delete_author(request):
ret = request.GET.get('id')
models.Author.objects.get(id=ret).delete()
return redirect('/author_list/')
2.寫入html文件:
<td><a href="/delete_author/?id={{ data.id }}">刪除</a></td>
步驟四:修改做者
1.寫入函數:
url(r'^edit_author/', views.edit_author),
def edit_author(request):
if request.method == 'POST':
edit_id = request.POST.get('edit_id')
print('------------')
print(edit_id)
print('------------------')
ret = request.POST.get('edit_name')
datas = request.POST.getlist('books')
print('=!'*200)
author = models.Author.objects.get(id=edit_id)
print('='*23)
author.name = ret
author.book.set(datas)
author.save()
return redirect('/author_list/')
ret = request.GET.get('id')
data = models.Author.objects.get(id=ret)
ret = models.Book.objects.all()
return render(request, 'edit_author.html',{'book_list': ret, 'data':data})
2.寫html文件:
<!DOCTYPE html>
<html>
<head>
<title>編輯做者</title>
</head>
<body>
<h1>編輯做者</h1>
<form action="/edit_author/" method="post">
<input type="text" name="edit_id" value='{{ data.id }}' style="display: none">
做者:<input type="text" name="edit_name" value="{{ data.name }}">
<select multiple name="books">
{% for book in book_list %}
{% if book in data.book.all %}
<option selected value="{{ book.id }}">{{ book.title }}</option>
{% else %}
<option value="{{ book.id }}">{{ book.title }}</option>
{% endif %}
{% endfor %}
</select>
<input type="submit" value="提交">
</form>
</body>
</html>