Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Jinja2条件在迭代时无法正常工作_Python_Google App Engine_Jinja2 - Fatal编程技术网

Python Jinja2条件在迭代时无法正常工作

Python Jinja2条件在迭代时无法正常工作,python,google-app-engine,jinja2,Python,Google App Engine,Jinja2,我用Python、Google应用程序引擎和Jinja2创建了一个博客。我有一个实体持有喜欢某个帖子的人,不包括帖子作者。我正在遍历所有喜欢帖子的用户,并对其进行比较,这样创建帖子的用户就不会喜欢自己的帖子。我还有一个按钮,只有当有人喜欢某个帖子时才会出现。我能够喜欢和不喜欢这个职位;用户喜欢一篇文章,而“不喜欢”按钮出现,问题是“喜欢”按钮不会消失,它出现在“不喜欢”按钮旁边,这会导致用户多次喜欢同一篇文章,并显示用户单击“喜欢”按钮时显示的“不喜欢”按钮的数量。变量username是当前登录

我用Python、Google应用程序引擎和Jinja2创建了一个博客。我有一个实体持有喜欢某个帖子的人,不包括帖子作者。我正在遍历所有喜欢帖子的用户,并对其进行比较,这样创建帖子的用户就不会喜欢自己的帖子。我还有一个按钮,只有当有人喜欢某个帖子时才会出现。我能够喜欢和不喜欢这个职位;用户喜欢一篇文章,而“不喜欢”按钮出现,问题是“喜欢”按钮不会消失,它出现在“不喜欢”按钮旁边,这会导致用户多次喜欢同一篇文章,并显示用户单击“喜欢”按钮时显示的“不喜欢”按钮的数量。变量
username
是当前登录的用户

{% for like in likes %} 
    {% if post.username != username and like.username != username %}
    <li>
        <form method="POST" id="likeForm">
            <input type="hidden" name="likePost" value="{{post.key().id()}}">
            <button type="submit" class="btn btn-outline-success btn-sm" form="likeForm">Like</button>
        </form>   
    </li>
    {% endif %}   
    {% if post.username != username and like.username == username %}
    <li>
        <form method="POST" id="unlikeForm">
            <input type="hidden" name="unlikePost" value="{{like.key().id()}}">
            <button type="submit" class="btn btn-outline-danger btn-sm" form="unlikeForm">Unlike</button>
        </form>   
    </li>
    {% endif %}
{% endfor %}
{like%中的like%
{%if post.username!=用户名和like.username!=用户名%}
  • 喜欢
  • {%endif%} {%if post.username!=用户名和like.username==用户名%}
  • 不像
  • {%endif%} {%endfor%}
    您误解了代码的行为。显示
    Like
    按钮的条件是:

    {% if post.username != username and like.username != username %}
    
    这意味着,除了
    用户名
    ,用户生成的
    喜欢
    列表中的每个
    喜欢
    都会显示一个
    喜欢
    按钮,而不管
    用户名
    是否喜欢该帖子。可能不是你想要的。您不应该在迭代
    likes
    时显示
    Like
    ,如果在迭代所有
    likes
    之后,您没有找到任何由
    用户名生成的内容,则只应显示一次

    至于多个
    不同的
    按钮-我怀疑当按下
    类似的
    按钮时,您没有在帖子上检查已经存在的
    类似的
    ,因此,允许在
    likes
    列表中有多个这样的
    like
    实例-每个实例都会导致显示一个
    like
    按钮

    我只想用python执行逻辑(比jinja2更容易,可能也更快):

    在金贾2:

    {% if post.username != username %}
        {% if post.liked %}
            <li>
                <form method="POST" id="unlikeForm">
                    <input type="hidden" name="unlikePost" value="{{like.key().id()}}">
                    <button type="submit" class="btn btn-outline-danger btn-sm" form="unlikeForm">Unlike</button>
                </form>   
            </li>
        {% else %}
            <li>
                <form method="POST" id="likeForm">
                    <input type="hidden" name="likePost" value="{{post.key().id()}}">
                    <button type="submit" class="btn btn-outline-success btn-sm" form="likeForm">Like</button>
                </form>   
            </li>
        {% endif %}
    {% endif %}
    
    {%if post.username!=username%}
    {%if post.liked%}
    
  • 不像
  • {%else%}
  • 喜欢
  • {%endif%} {%endif%}
    注意:未实际测试的代码段

    {% if post.username != username %}
        {% if post.liked %}
            <li>
                <form method="POST" id="unlikeForm">
                    <input type="hidden" name="unlikePost" value="{{like.key().id()}}">
                    <button type="submit" class="btn btn-outline-danger btn-sm" form="unlikeForm">Unlike</button>
                </form>   
            </li>
        {% else %}
            <li>
                <form method="POST" id="likeForm">
                    <input type="hidden" name="likePost" value="{{post.key().id()}}">
                    <button type="submit" class="btn btn-outline-success btn-sm" form="likeForm">Like</button>
                </form>   
            </li>
        {% endif %}
    {% endif %}