我正在尝试显示一个列表页面。尽管显示得很好,django仍然给出了如下错误:
raise self.model.DoesNotExist( auctions.models.Listing.DoesNotExist:列表匹配查询不存在)。
索引视图通过迭代显示所有列表,清单视图通过接收变量"form.title“来显示特定的列表视图。
我的代码如下:
views.py:
def listing_view(request, title):
# if get a listing page
categories = Category.objects.all()
# Get all the forms submitted
forms = Listing.objects.all()
# get the highest bid for a listing
highest_price = Bid.objects.filter(title=title).order_by("-bid_price").first()
# Get the listing with the title
listing = Listing.objects.get(title=title)
if highest_price is not None:
listing.current_price = highest_price.bid_price
# else set starting price as the current price
else:
listing.current_price = listing.starting_price
listing.save()index.html:
<!-- image of a listing -->
<div class="col-4">
<a href="{% url 'listing_view' form.title %}">
{% if form.image %}
<img src="{{ form.image.url }}" alt="" width="267px" />
{% elif form.image_url %}
<img src="{{ form.image_url }}" alt="" width="267px" />
{% endif %}
</a>
</div>listing.html:
<!-- image in the left of the container -->
<div class="col-4">
{% if listing.image %}
<img src="{{ listing.image.url }}" alt="" width="267px" />
{% elif listing.image_url %}
<img src="{{ listing.image_url }}" alt="" width="267px" />
{% endif %}
</div>这里的html文件是为了澄清在列表页面显示中需要变量“exist”,因为它说不存在,所以我尝试使用try和of来代替语句,但是它给了我另一个错误,如下所示:
UnboundLocalError:赋值前引用的局部变量“列表”
我知道我使它成为一个局部变量,所以返回呈现无法到达它。
但是,如何在没有错误的情况下获得列表对象呢?
任何帮助都很感激!
谢谢!
发布于 2020-09-05 11:25:01
可能存在与更多事情相关的错误,请确保您已经完成了迁移,并通过
python manage.py makemigrations
python manage.py migrate如果这样做是正确的,那么确保在视图的返回值中,listing_view在上下文中传递如下所示
return render(request, 'yourdir/index.html)https://stackoverflow.com/questions/63751887
复制相似问题