+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于2021-03-06 19:30     阅读(144)     评论(0)     点赞(23)     收藏(3)


0

1

2

3

4

5

6

7

8

9

Here is my template, the if statements aren't working!

{% 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

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

If you need more information ask me in the comments!

It doesn't show the content of the if statements! 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
        })

0

1

2

3

4

5

6

7

8



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/879599/8ec440a7a49f4f8c2cf8/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)