视图似乎没有将所有信息保存在我的模型中 [英] View does not seem to be saving all the information in my model
本文介绍了视图似乎没有将所有信息保存在我的模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用于保存书籍的模型和视图。但是,当我访问该信息时,它似乎并不完整。所有查询似乎都在正常运行。我不知所措
I have a model and a view for saving books. However, when I access the information it does not appear to be complete. All queries seem to be running properly. I'm at a loss what could be going wrong
我的模型
class Book(models.Model):
title = models.CharField(max_length=50)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE,)
cover_pic = models.FileField(null=True, blank=True)
author = models.CharField(max_length=100, blank=True)
description = models.TextField(max_length=1000)
uploaded_on = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("books:addBook")
class Meta:
ordering = ["-uploaded_on"]
我的 views.py
class addBook(FormUserNeededMixin, CreateView):
form_class = BookForm
success_url = reverse_lazy('addbook')
template_name = 'books/addbook.html'
forms.py
class BookForm(forms.ModelForm):
class Meta:
model = Book
exclude = ['user', 'uploaded_on']
am尝试使用此视图访问详细信息
am trying to access the details using this view
class BookListView(ListView):
model = Book
template_name = 'books/viewbooks.html'
和此模板
{% for books in object_list %}
<img src="{{ book.cover_pic }}">
<h5>{{ book.title }}</h5>
<p>{{ book.author }}</p>
{% endfor %}
每次添加项目时,我都会看到添加的另一个列表项目在视图书中,但没有我请求的详细信息。
everytime I add an item I can see another list item added in view books but has none of the details that I requested.
推荐答案
您的循环变量是 books
不是 book
。重命名其中之一,例如for标记中的一个:
Your loop variable is books
not book
. Rename one of them, for example the one in the for tag:
{% for book in object_list %}
<img src="{{ book.cover_pic }}">
<h5>{{ book.title }}</h5>
<p>{{ book.author }}</p>
{% endfor %}
这篇关于视图似乎没有将所有信息保存在我的模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文