【问题标题】:Can't figure out the error on my jinja2/django template无法找出我的 jinja2/django 模板上的错误
【发布时间】:2021-03-29 22:03:26
【问题描述】:

这是我的模板,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 %}

这是我的意见.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,
            })
我无法弄清楚错误在哪里。如何修复此错误以及如何避免此类错误? Django版本:Django 3.1.1,Python版本:Python 3.8.5

如果您需要更多信息,请在 cmets 中问我!

它不显示 if 语句的内容!我不知道为什么。视图正在正确发送它检查过的数据!

【问题讨论】:

    标签: python html python-3.x django jinja2


    【解决方案1】:

    修复了使它们成为两个字符串所需的错误

    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
            })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-07
      • 2022-01-12
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      相关资源
      最近更新 更多