models.py
class UserInfo(models.Model):
username = models.CharField(max_length=64,db_column='username')
passwd = models.CharField(max_length=64,db_column='password')
register_date = models.DateTimeField(max_length=32,db_column='register_date')
last_login_date = models.DateTimeField(max_length=32,db_column='last_login_date',null=True)
class Meta:
app_label = 'simple'
db_table = 'user_info'
class ServerInfo(models.Model):
hostname = models.CharField(max_length=32,db_column='hostname')
port = models.IntegerField(max_length=32,db_column='port')
status = models.CharField(max_length=32,db_column='status')
class Meta:
app_label = 'simple'
db_table = 'server_info'
class Publisher(models.Model):
name = models.CharField(max_length=32)
address = models.CharField(max_length=32)
state = models.CharField(max_length=32)
nation = models.CharField(max_length=32)
website = models.URLField(max_length=32)
class Author(models.Model):
first_name = models.CharField(max_length=32)
last_name = models.CharField(max_length=32)
email = models.EmailField()
class Book(models.Model):
title = models.CharField(max_length=32,unique=True)
author = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
pubDate = models.CharField(max_length=32)
views.py
def addBook(request):
if request.method == 'POST':
print("進入addbook post")
print(request.POST)
name = request.POST['name']
author = request.POST.getlist('author')# 當獲取多個數值的時候,使用getlist
publisher = request.POST['publisher']
p1 = Publisher.objects.get(id=publisher)#先在publisher表中查詢出前端選中出版社對應的對象
date = request.POST['publisherDate']
b1 = Book(title=name,publisher=p1,pubDate=date)
b1.save()#普通插入的數據和外鍵插入的數據須要先save()
b1 = Book.objects.get(title=name)#查出書名對象,也就是獲取要插入的多對多數據項
if len(author) == 1:
b1.author.add(author[0])#多對多使用add方法進行插入
b1.save()
return redirect("/displayBook/")
elif len(author) == 0:#當用戶沒有選中做者
return render(request,'addBook.html',{"status":"添加出版社失敗,沒有選擇做者"})
else:#循環插入用戶選中的多個做者
for person in author:
b1.author.add(person)#多對多使用add方法進行插入
b1.save()
return redirect("/displayBook/")
print("進入addbook get") #用戶從庫中獲取頁面可選的內容,get請求
bookList = Book.objects.all()
publisherList = Publisher.objects.all()
authorList = Author.objects.all()
print(bookList,publisherList,authorList)
return render(request, 'addBook.html',{'books':bookList,
'publishers':publisherList,
'authors':authorList})
前端頁面:
{% block head-menu %}
<table border=1 style="margin-left:13%;margin-top: 3%;height: 30px;width: 1000px">
<tr >
<td>書名</td>
<td>做者</td>
<td>出版社</td>
<td>出版日期</td>
</tr>
{% for book in books %}
{% if forloop.counter|divisibleby:"2" %}
<tr style="background-color: skyblue;">
<td>{{ book.title }}</td>
<td>{% for authorObj in book.author.select_related %} {{ authorObj.first_name }} {{ authorObj.last_name }} {% endfor %}</td>
<td>{{ book.publisher.name }}</td>
<td>{{ book.pubDate }}</td>
</tr>
</tr>
</tr>
{% else %}
<tr style="background-color: salmon;">
<td>{{ book.title }}</td>
<td>{% for authorObj in book.author.select_related %} {{ authorObj.first_name }} {{ authorObj.last_name }} {% endfor %}</td>
<td>{{ book.publisher.name }}</td>
<td>{{ book.pubDate }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
<a href="/backend/"><button type="button" class="btn btn-primary btn-sm" style="margin-left: 43%;margin-top: 10%">返回</button></a>
{% endblock %}