作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试输入一些书籍数据并使用 django 将其保存到我的数据库中(试图制作一个网络图书馆)
但是我遇到了这种错误:
初始化 () 得到了一个意外的关键字参数“book_category”
错误似乎在这行代码中:
form = CategoryForm(book_category= book_category)
这是我的 views.py 的代码
class AddBook(View):
def post(self, request):
form = BooksForm(request.POST, request.FILES)
if form.is_valid()
book_category = request.POST.get('book_category')
firstname = request.POST.get('Firstname')
lastname = request.POST.get('Lastname')
author = Author.objects.filter(Q(firstname__icontains = firstname) & Q(lastname__icontains = lastname))
if author:
print(author)
else:
form = AuthorForm(firstname=firstname, lastname=lastname)
form.save()
category = Category.objects.filter(Q(book_category__icontains = book_category))
if category:
print(category)
else:
form = CategoryForm(book_category= book_category)
form.save()
author = Author.objects.filter(Q(firstname__icontains = firstname) & Q(lastname__icontains = lastname))
for a in author:
print(a.book_author_id)
for c in category:
print(c.book_category_no)
book_title = request.POST.get('book_title')
book_cover = request.FILES.get('book_cover')
book_file = request.FILES.get('book_file')
book_year = request.POST.get('book_year')
book_tags = request.POST.get('book_tags')
book_summary = request.POST.get('book_summary')
form = Books(book_title = book_title, book_author_id = Author.objects.get(book_author_id = a.book_author_id), book_cover = book_cover,
book_file = book_file, book_year = book_year, book_summary = book_summary, book_category_no = Category.objects.get(book_category_no = c.book_category_no),
is_bookmarked = 0, is_downloaded = 0, is_read = 0, is_deleted = 0)
form.save()
return HttpResponse('Book Saved!')
else:
print(form.errors)
return HttpResponse('Not Valid')
这是我的models.py:
class Category(models.Model):
book_category_no = models.AutoField(primary_key=True)
book_category = models.CharField(max_length=100)
class Meta:
db_table = "Category"
class Books(models.Model):
book_id = models.AutoField(primary_key=True)
book_title = models.CharField(max_length = 100)
book_author_id = models.ForeignKey(Author, on_delete=models.CASCADE)
book_cover = models.ImageField(upload_to='media/')
book_file = models.FileField(upload_to='media/')
book_year = models.DateField()
book_tags = models.CharField(max_length = 100)
book_summary = models.CharField(max_length = 100)
book_category_no = models.ForeignKey(Category, on_delete=models.CASCADE)
date_added = models.DateField(default=timezone.now)
# book_info = models.CharField(max_length = 100, default="")
is_bookmarked = models.BooleanField()
is_downloaded = models.BooleanField()
is_read = models.BooleanField()
is_deleted= models.BooleanField(default=False)
class Meta:
db_table = "Books"
我不知道发生了什么事,我是 Web 开发新手,我已经坚持了好几天了。我真的很感激一些帮助:)
最佳答案
当您调用以下内容时,问题出在代码中的多个实例中:
book_category__icontains
从您的模型中可以看出,您没有
book_category
领域,你拥有的是
book_category_no
field 。
class Category(models.Model):
# book_category_no = models.AutoField(primary_key=True) # no need for this.
# book_category = models.CharField(max_length=100)
name = models.CharField(max_length=100
class Meta:
db_table = "Category"
class Books(models.Model):
#book_id = models.AutoField(primary_key=True)# no need for this
title = models.CharField(max_length = 100)
author= models.ForeignKey(Author, on_delete=models.CASCADE)# do not use author_id, use just author
# similarly:
cover = models.ImageField(upload_to='media/')
file = models.FileField(upload_to='media/')
year = models.DateField()
tags = models.CharField(max_length = 100)
summary = models.CharField(max_length = 100)
#when referring foreignkey, no need to add _no or _id after the name.
category = models.ForeignKey(Category, on_delete=models.CASCADE)
date_added = models.DateField(default=timezone.now)
# book_info = models.CharField(max_length = 100, default="")
is_bookmarked = models.BooleanField()
is_downloaded = models.BooleanField()
is_read = models.BooleanField()
is_deleted= models.BooleanField(default=False)
class Meta:
db_table = "Books"
关于python - 我如何处理这个错误 : __init__() got an unexpected keyword argument 'book_category' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65191990/
我正在尝试输入一些书籍数据并使用 django 将其保存到我的数据库中(试图制作一个网络图书馆) 但是我遇到了这种错误: 初始化 () 得到了一个意外的关键字参数“book_category” 错误似
我有两个表名称 book_list 和 book_category。架构如下: book_list +---------+--------+-----------+ | id_book | id_c
我是一名优秀的程序员,十分优秀!