如何访问 Django 中基于 class 的视图中的多对多字段?
How to Access Many to many field in class based views in Django?
我在 models.py
中有两个模型,作者和书籍
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
age = models.IntegerField()
def __str__(self):
return '%s %s' %(self.first_name, self.last_name)
def __unicode__(self):
return '%s %s' %(self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100) #name = title
pages = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2)
rating = models.FloatField()
author = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __str__(self):
return self.title
def __unicode__(self):
return self.title
现在我正在使用 ListView 列出所有书籍。单击一本书,我使用以下方法获取该书的信息
class BookDetailView(generic.DetailView):
template_name = 'books/book_detail1.html'
model = Book
context_object_name = 'book_detail'
我可以访问标题、页面、价格、评级、出版商和 publication_date 但无法获取所有作者(作者列表)。虽然我打算简单地打印它,但它会在模板中打印 None。我什至尝试使用 For Loop 进行迭代,但没有在模板
中完成
views.py
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<ul>
<li>Price:{{ book_detail.price }}</li>
<li>Pub Date:{{ book_detail.publication_date }}</li>
<li>Title: {{ book_detail.title }}</li>
<li>Pages: {{ book_detail.pages }}</li>
<li>Rating: {{ book_detail.rating }}</li>
<li>Publisher: {{ book_detail.publisher }}</li>
<li>Author: {{ book_detail.author }}</li>
</ul>
</body>
</html>
谁能帮我解决这个问题?
您在 Book
和 Author
之间定义了多对多关系,这意味着一本书可以有任意数量的作者。为了显示它们,您需要遍历作者集:
Authors:
<ul>
{% for author in book_detail.author.all %}
<li>{{ author }}</li>
{% endfor %}
</ul>
您可能希望将该字段的名称更改为 authors
以减少混淆。
或者,如果您只想让一位作者写一本书,那么您需要使用 ForeignKey
而不是 ManyToManyField
。在那种情况下,您现有的模板逻辑将起作用。
或者,如果您使用 Function-Based 视图 ,请定义此视图:
#views.py
def book_detail_view(request, id):
book = get_object_or_404(Book, id=id)
authors_of_book = book.questions.all()
template = 'books/book_detail1.html'
context = {'authors_of_book': authors_of_book}
return render(request, template, context)
还有你的模板:
#html.py
<ul>
{% for author in authors_of_book %}
<li>{{ author }}</li>
{% endfor %}
</ul>
For more detail read this document.
我在 models.py
中有两个模型,作者和书籍class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
age = models.IntegerField()
def __str__(self):
return '%s %s' %(self.first_name, self.last_name)
def __unicode__(self):
return '%s %s' %(self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100) #name = title
pages = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2)
rating = models.FloatField()
author = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __str__(self):
return self.title
def __unicode__(self):
return self.title
现在我正在使用 ListView 列出所有书籍。单击一本书,我使用以下方法获取该书的信息
class BookDetailView(generic.DetailView):
template_name = 'books/book_detail1.html'
model = Book
context_object_name = 'book_detail'
我可以访问标题、页面、价格、评级、出版商和 publication_date 但无法获取所有作者(作者列表)。虽然我打算简单地打印它,但它会在模板中打印 None。我什至尝试使用 For Loop 进行迭代,但没有在模板
中完成views.py
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<ul>
<li>Price:{{ book_detail.price }}</li>
<li>Pub Date:{{ book_detail.publication_date }}</li>
<li>Title: {{ book_detail.title }}</li>
<li>Pages: {{ book_detail.pages }}</li>
<li>Rating: {{ book_detail.rating }}</li>
<li>Publisher: {{ book_detail.publisher }}</li>
<li>Author: {{ book_detail.author }}</li>
</ul>
</body>
</html>
谁能帮我解决这个问题?
您在 Book
和 Author
之间定义了多对多关系,这意味着一本书可以有任意数量的作者。为了显示它们,您需要遍历作者集:
Authors:
<ul>
{% for author in book_detail.author.all %}
<li>{{ author }}</li>
{% endfor %}
</ul>
您可能希望将该字段的名称更改为 authors
以减少混淆。
或者,如果您只想让一位作者写一本书,那么您需要使用 ForeignKey
而不是 ManyToManyField
。在那种情况下,您现有的模板逻辑将起作用。
或者,如果您使用 Function-Based 视图 ,请定义此视图:
#views.py
def book_detail_view(request, id):
book = get_object_or_404(Book, id=id)
authors_of_book = book.questions.all()
template = 'books/book_detail1.html'
context = {'authors_of_book': authors_of_book}
return render(request, template, context)
还有你的模板:
#html.py
<ul>
{% for author in authors_of_book %}
<li>{{ author }}</li>
{% endfor %}
</ul>
For more detail read this document.