Python烧瓶:{%}内的变量

Python烧瓶:{%}内的变量,python,python-3.x,flask,Python,Python 3.x,Flask,我做了一条路线,基本上是这样的: @app.route("/articles/<string:id>") def articleByid (id): return render_template('subject.html', id=id, articles=Articles) @app.route(“/articles/”) def articleByid(id): 返回呈现模板('subject.html',id=id,articles=articles) subject.h

我做了一条路线,基本上是这样的:

@app.route("/articles/<string:id>")
def articleByid (id):
return render_template('subject.html', id=id, articles=Articles)
@app.route(“/articles/”)
def articleByid(id):
返回呈现模板('subject.html',id=id,articles=articles)
subject.html:

{% for article in articles %}
    {% if article.id == {{ id }} %}
        <h2>[ {{ article['id'] }} ] - {{ article['title'] }}</h2><br>
        <p>Written by : {{ article['author'] }}</p><br>
        <h4>{{ article['body'] }}</h4><br>
        <p> -| {{ article['create_date'] }} |-</p><br>
    {% endif %}
{% endfor %}
{%用于文章%中的文章]
{%if article.id=={{id}}%}
[{{article['id']}}]-{{{article['title']}}}
作者:{{文章['author']}


{{文章['body']}}
-|{article['create_date']}}|-


{%endif%} {%endfor%}
问题是,它对我大喊大叫,因为它期待着:

{%if article.id=={{id}}%}


我正确地通过了我的字典,它很好用。。我还将id作为字符串传递。。基本上我想从url中获取id调用的主题的内容,我不知道为什么它不起作用

您正在尝试比较字符串和int。
article.id
是int,而
id
变量是字符串

{% if article.id == int(id) %}
或者,将id参数作为int传递

@app.route("/articles/<int:id>")
def articleByid (id=None):

>>> a = 1
>>> s = '1'
>>> a == s
False
>>> type(a)
<class 'int'>
>>> type(s)
<class 'str'>
>>>
@app.route(“/articles/”)
def articleByid(id=None):
>>>a=1
>>>s='1'
>>>a==s
错误的
>>>类型(a)
>>>类型
>>>
更新:我已经尝试运行上面发布的代码,它可以正常工作。如果你得到一个错误,它一定是不同的东西以外,你已经张贴

>>> import jinja2
>>> t = jinja2.Template("{% for article in articles %} {% if article.id == id %} <p>{{ article.id }} , {{ article.title }}</p> {% endif %} {% endfor %}")
>>> articles = [dict(id=2, title='Two'), dict(id=1, title='One')]
>>> print (t.render(articles=articles, id=1))
    <p>1 , One</p>
>>> print (t.render(articles=articles, id=2))
    <p>2 , Two</p>
>>> x = 2
>>> print (t.render(articles=articles, id=x))
    <p>2 , Two</p>
>>导入金玉2
>>>t=jinja2.Template(“{%for article%}{%if article.id==id%}{article.id},{{article.title}

{%endif%}{%endfor%}”) >>>文章=[dict(id=2,title='2'),dict(id=1,title='1')] >>>打印(t.render(文章=文章,id=1)) 一,一

>>>打印(t.render(文章=文章,id=2)) 2,2

>>>x=2 >>>打印(t.render(articles=articles,id=x)) 2,2


您正在尝试比较字符串和int。
article.id
是int,而
id
变量是字符串

{% if article.id == int(id) %}
或者,将id参数作为int传递

@app.route("/articles/<int:id>")
def articleByid (id=None):

>>> a = 1
>>> s = '1'
>>> a == s
False
>>> type(a)
<class 'int'>
>>> type(s)
<class 'str'>
>>>
@app.route(“/articles/”)
def articleByid(id=None):
>>>a=1
>>>s='1'
>>>a==s
错误的
>>>类型(a)
>>>类型
>>>
更新:我已经尝试运行上面发布的代码,它可以正常工作。如果你得到一个错误,它一定是不同的东西以外,你已经张贴

>>> import jinja2
>>> t = jinja2.Template("{% for article in articles %} {% if article.id == id %} <p>{{ article.id }} , {{ article.title }}</p> {% endif %} {% endfor %}")
>>> articles = [dict(id=2, title='Two'), dict(id=1, title='One')]
>>> print (t.render(articles=articles, id=1))
    <p>1 , One</p>
>>> print (t.render(articles=articles, id=2))
    <p>2 , Two</p>
>>> x = 2
>>> print (t.render(articles=articles, id=x))
    <p>2 , Two</p>
>>导入金玉2
>>>t=jinja2.Template(“{%for article%}{%if article.id==id%}{article.id},{{article.title}

{%endif%}{%endfor%}”) >>>文章=[dict(id=2,title='2'),dict(id=1,title='1')] >>>打印(t.render(文章=文章,id=1)) 一,一

>>>打印(t.render(文章=文章,id=2)) 2,2

>>>x=2 >>>打印(t.render(articles=articles,id=x)) 2,2


将文章列表和id传递到模板中以使用id选择其中一篇文章是有问题的。你使用的内存与文章的数量成正比。随着这个数字的增长,内存的使用也在增加

考虑在处理程序中进行选择,将单个项目传递到模板中。就是说,

@app.route("/articles/<int:id>")
def articleById(id):
    try:
        article = Articles.query.get(id)
        return render_template('subject.html', article=article)
    except:
        # handle missing article, possibly via flask.abort(404)
@app.route(“/articles/”)
def articleById(id):
尝试:
article=Articles.query.get(id)
返回呈现模板('subject.html',article=article)
除:
#处理丢失的物品,可能通过烧瓶。中止(404)

将文章列表和id传递到模板中以使用id选择其中一篇文章是有问题的。你使用的内存与文章的数量成正比。随着这个数字的增长,内存的使用也在增加

考虑在处理程序中进行选择,将单个项目传递到模板中。就是说,

@app.route("/articles/<int:id>")
def articleById(id):
    try:
        article = Articles.query.get(id)
        return render_template('subject.html', article=article)
    except:
        # handle missing article, possibly via flask.abort(404)
@app.route(“/articles/”)
def articleById(id):
尝试:
article=Articles.query.get(id)
返回呈现模板('subject.html',article=article)
除:
#处理丢失的物品,可能通过烧瓶。中止(404)

由于您似乎只想显示具有特定id的一篇文章,因此使用
articlebyId
函数选择特定文章,然后将单个文章对象传递给模板将省力且代码更简洁。感谢您的快速回复,但我发布这篇文章是为了了解和理解我到底遇到了什么问题!没问题,我只是想我应该指出一个更好的方法来避免你遇到的任何问题。您收到的确切错误消息是什么?再次感谢您的理解!问题就在这一行代码中:
{%if article.id=={{id}%}
它在==之后需要一个表达式。您是否找到了导致
在==
之后需要一个表达式的原因?因为您似乎只想显示具有特定id的一篇文章,这将减少工作量,并且代码更干净,使用
articlebyId
函数选择特定文章,然后将单个文章对象传递给模板。感谢您的快速回复,但我发布此消息是为了了解和理解我到底遇到了什么问题!没问题,我只是想我应该指出一个更好的方法来避免你遇到的任何问题。您收到的确切错误消息是什么?再次感谢您的理解!问题就在这行代码中:
{%if article.id=={{id}}%}
它在==之后需要一个表达式。您是否找到了导致
在==
之后需要一个表达式的原因?嘿,谢谢!但是仍然不起作用,{%if article.id==int(id)%}不是变量,应该在{id}中使用。{code>{%if article.id==int(id)%}使用它或:
str(article.id)==id
#如果id是字符串,则不会给出任何错误,但不起作用!现在当我这样做时:
{%if article.id==1%}