首页 技术 正文
技术 2022年11月15日
0 收藏 891 点赞 4,786 浏览 3041 个字

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/" rel="external nofollow" ><button type="button" class="btn btn-primary btn-sm" style="margin-left: 43%;margin-top: 10%">返回</button></a>
{% endblock %}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898