【问题标题】:How do i set the length of like counts我如何设置喜欢计数的长度
【发布时间】:2020-08-20 04:05:13
【问题描述】:

我是 Django 的新手,我正在开发一个用户喜欢帖子的网站。我有一个关于如何在删除“喜欢”并仅显示点赞数之前设置点赞数长度的问题。当用户不点击“Like”按钮时,它会在“Like”按钮的侧面显示“LIKE”,但是当用户单击“Like”按钮时,它会在计数侧面显示“LIKE”而不是“LIKE”,因为我我正在使用 else 语句。我如何在喜欢计数的一侧显示“喜欢”。例如; '1 Liked' 然后,当 100 个用户点击 Like 按钮时,喜欢计数:'100'。但如果少于 100,点赞数将是 '99 Liked'

image to help

{% if post.liked_by_user %}
    <button type="submit" name="post_id" value="{{ post.id }}" class="like float-left pl-2" >
    <img src="{{ '/static/' }}images/heart_empty.png" width="24" height="24">
    </button>
    {% if post.likes.all %}
        <a href="{% url 'site:likes_users' post.id %}" class="like-count font-small dark-grey-text font-weight-bold">
        <span class="d-inline-block text-truncate" style="max-width:70px;position:relative;top:3px;">
        {{ post.likes.count }} 
        </span>
        </a>
    {% else %}
        <span class="like-count font-small dark-grey-text font-weight-bold">Like</span>
    {% endif %}
{% endif %}

【问题讨论】:

    标签: python html django jinja2


    【解决方案1】:

    您可以使用另一个if 语句来了解点赞数何时小于100。如果是则将"Liked" 放在它旁边,如果不是则不要。

    {% if post.liked_by_user %}
        <button type="submit" name="post_id" value="{{ post.id }}" class="like float-left pl-2" >
        <img src="{{ '/static/' }}images/heart_empty.png" width="24" height="24">
        </button>
        {% if post.likes.all %}
            {% if post.likes.count >= 100 %}
                <a href="{% url 'site:likes_users' post.id %}" class="like-count font-small dark-grey-text font-weight-bold">
    
                <span class="d-inline-block text-truncate" style="max-width:70px;position:relative;top:3px;">
                {{ post.likes.count }} 
                </span>
                </a>
            {% else %}
                <a href="{% url 'site:likes_users' post.id %}" class="like-count font-small dark-grey-text font-weight-bold">
    
                <span class="d-inline-block text-truncate" style="max-width:70px;position:relative;top:3px;">
                {{ post.likes.count }} Liked
                </span>
                </a>
            {% endif %}
        {% else %}
            <span class="like-count font-small dark-grey-text font-weight-bold">Like</span>
        {% endif %}
    {% endif %}
    

    这是我用 Flask 制作的一个简单的应用程序来帮助你:

    templates/base.html

    <!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
    </head>
    <body>
        {% if likes %}
            {% if likes < 5 %}
                <span>{{ likes }} Liked</span>
            {% else %}
                <span>{{ likes }}</span>
            {% endif %}
        {% else %}
            <span>Like</span>
        {% endif %}
    </body>
    </html>
    

    app.py

    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    @app.route("/")
    def homepage():
        likes = 4
        return render_template("base.html", likes=likes)
    
    if __name__ == '__main__':
        app.run(host="localhost", port="5000")
    

    当likes 小于5 时,例如4,则打印输出为4 Liked。当它等于或大于5 时,只打印点赞数。

    【讨论】:

    • @Fillip..this 没有显示任何内容。它仍然显示小于 100 的数字而没有喜欢。
    • @MrHize 哦,对不起,我倒退了。大于 100 的数字将在其旁边显示为“喜欢”。我制作了 Flask 应用程序来帮助你自己做,所以你可以理解我做了什么。我编辑了答案。
    • @MrHize 没问题 :)
    猜你喜欢
    • 2011-08-14
    • 2020-05-30
    • 2023-03-18
    • 2022-06-22
    • 2012-08-02
    • 2012-08-27
    • 1970-01-01
    • 2018-10-04
    • 2019-10-06
    相关资源
    最近更新 更多