简体   繁体   English

无法找出我的 jinja2/django 模板上的错误

[英]Can't figure out the error on my jinja2/django template

Here is my template, the if statements aren't working!这是我的模板,if 语句不起作用!

{% extends "auctions/layout.html" %}    
{% block body %}    
{% if bid.name == User %}
<br>
<h1>You Won the auction</h1>
<br>
{% endif %}
<h1>Title: {{title}}</h1>
<br>    
<h1>Seller: {{listing.name}}</h1>
<br>    
<h1>Description: {{listing.description}}</h1>
<br>    
<h1>Category: {{listing.category}}</h1>
<br>    
<h1>price: {{bid.price}} by {{bid.name}}</h1>
<br>    
<form  action="{% url 'Bid' %}" method="post">
   {% csrf_token %}
   <input type="hidden" name="title" value="{{title}}">
   <input type="number" name="price">
   <input type="submit">
</form>
<form  action="{% url 'watchlist'%}" method="post">
   { % csrf_token %}
   Add to watchlist
   <input type="hidden" name="form_type" value="Watchlist">
   <input type="hidden" name="title" value="{{title}}">
   <input type="submit">
</form>
{% if User == "ilias" %}
<br>
<h1>Close the auction</h1>
<br>
<form  action="{% url 'close' %}" method="post">
   {% csrf_token %}
   <input type="hidden" name="title" value="{{title}}">
   <input type="submit">
</form>
<br>
{% endif %}
<form  action="{% url 'comment'%}" method="post">
   {% csrf_token %}
   <h1>Add comment</h1>
   <input type="text" name="content">
   <input type="hidden" name="title" value="{{title}}">
   <input type="submit">
</form>
{%for item in comments%}
<h3>{{item.content}} by {{item.name}}</h3>
<br>
{%endfor%}
{% endblock %}

Here is my views.py这是我的意见.py

def listings(request, title):
    try:
        listing = Listing.objects.get(title=title)
    except:
        return HttpResponse('<h3>Page not found</h3>')
    else:
        title = listing.title
        name = listing.name
        comments = comment.objects.filter(title=title)
        bid = Bid.objects.get(title=title)
        User = request.user.username
        print name
        return render(request, 'auctions/listing.html', {
            'listing': listing,
            'title': title,
            'comments': comments,
            'bid': bid,
            'User': User,
            'name': name,
            })
I can't figure out where is the error. 我无法弄清楚错误在哪里。 How can I fix this error and how to avoid this types of errors? 如何修复此错误以及如何避免此类错误? Django version: Django 3.1.1, Python version: Python 3.8.5 Django 版本:Django 3.1.1、Python 版本:ZA7F5F35423.8B927421173B9274217329

If you need more information ask me in the comments!如果您需要更多信息,请在评论中问我!

It doesn't show the content of the if statements. 它不显示 if 语句的内容。 I don't know why. 我不知道为什么。 The views is properly sending data it checked it! 视图正在正确发送它检查过的数据!

Fixed the error just needed to make them both strings修复了使它们成为两个字符串所需的错误

def listings(request, title):
    try:
        listing = Listing.objects.get(title=title) 
    except:
        return HttpResponse('<h3>Page not found</h3>')
    else:
        title = listing.title   
        name = str(listing.name)
        comments = comment.objects.filter(title=title)
        bid = Bid.objects.get(title=title)
        User = str(request.user.username)
        bidder = str(bid.name)
        print(name)
        return render(request, "auctions/listing.html", {   
            "listing":listing,
            "title":title,
            "comments":comments,
            "bid":bid,
            "User":User,
            "name":name,
            "bidder":bidder
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM