【问题标题】:How to get unique count in Python?如何在 Python 中获得唯一计数?
【发布时间】:2015-11-03 18:23:07
【问题描述】:

我正在制作一个简单的应用程序,您可以在其中对不同的帖子投反对票和赞成票。我在 Python/Django 中构建它...我通过将用户 id 和评论 id 存储到 Upvote 模型中来获得 upvote/downvote 计数,并且通过关联计数为 1。它在打印出正确的数字查看除了打印出计数被赞成的次数。例如,如果它是 1,它将打印:1,但如果它是 3,它将打印:3 3 3。我如何更改我的逻辑以便它只打印一次数字?

查看页面:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Topic</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
    <a href="/dashboard">Dashboard</a>
    <a href="/logout">Logout</a>
    <hr>
        <a href="/users/{{topic.user.id}}/">{{topic.user.first_name}} </a>posted a topic:
        <hr>
        Topic: {{topic.topic}}
        <br>
        Description: {{topic.description}}
        <hr>
        <h3>Post your answer</h3>
        <form action="/topic/{{topic.id}}/post" method="post">
            {%csrf_token%}
            <textarea name="comment" id="" cols="20" rows="2" required style="resize:none"></textarea>
            <br>
            <input type="submit" value="Post">
        </form>
        <hr>
        {% for comment in comments %}
            <a href="/users/{{comment.user_id}}/">{{comment.user.first_name}}</a>: {{comment.comment}}
            <br>
                <a href="/delete/{{comment.id}}/">Delete Post</a>
                <h5>Number of upvotes:</h5>
            {% for upvote in upvotes %}
                {% if upvote.comment_id == comment.id %}
                        {{upvotes.count}}
                {%endif%}
            {%endfor%}
                <h5>Number of downvotes: </h5>
            {% for downvote in downvotes %}
                {% if downvote.comment_id == comment.id %}
                    {{downvotes.count}}
                {%endif%}
            {%endfor%}  
            <hr>
            <a href="/upvote/{{comment.id}}/"><button>Upvote</button></a>
            <a href="/downvote/{{comment.id}}/"><button>Downvote</button></a>
            <br>
            <form action="/idea/{{comment.id}}/" method="post">
            {%csrf_token%}
            <textarea name="idea" id="" cols="20" rows="2" required style="resize:none"></textarea>
            <br>
            <input type="submit" value="Comment">
            </form>
                {% for idea in ideas %}
                    {% if idea.comment_id == comment.id %}
                        <a href="/users/{{idea.user_id}}/">{{idea.user}}</a> 
                        says: {{idea.idea}}
                        <a href="/delete/{{idea.id}}/comment">Delete Comment</a>
                    {%endif%}
                {%endfor%}
        {%endfor%}
    </div>
</body>
</html>

【问题讨论】:

    标签: python django count


    【解决方案1】:

    给定你的代码

        {% for upvote in upvotes %}
                {% if upvote.comment_id == comment.id %}
                        {{upvotes.count}}
                {%endif%}
        {%endfor%}
    

    你最终打印了upvotes.count upvotes 次。您可以完全摆脱整个 for 循环,只需使用 {{upvotes.count}}

    【讨论】:

    • 但不同的 cmets 有多个支持计数。所以在views.py中,当我获取upvotes时,我获取upvotes = Upvote.objects.all(),然后在我的模板中检查upvote.comment_id == comment.id,如果是,那么我将其打印出来。所以这行不通?
    • 也许您应该在应用程序逻辑中而不是在视图中进行计数。取一个不同的变量,在upvote.comment_id == comment.id 时将其递增,然后将其传递给您的视图。否则,您需要在视图中增加一个不同的变量,然后在循环完成时打印它。
    • 我支持上面的想法,我只会计算 Upvote.objects.all() 的数量并传递一个带有该数量视图的变量。
    • 当..在视图中真的没有其他方法可以做到这一点..我正在尝试在函数中创建一个递增但似乎不起作用的计数器:
    【解决方案2】:

    这是我现在的函数...试图在名为 counter 的函数中增加一个变量..我在模型中将它作为一个默认值 = 0 的整数字段

    def upvote(request, comment_id):
        print "upvoting comment"
        user = User.objects.get(id=request.session['user_id'])
        comment = Comment.objects.get(id=comment_id)
        upvote = Upvote()
        counter = 1
        upvote.comment = comment
        upvote.user = user
        upvote.created_at = timezone.now()
        upvote.counter = upvote.counter + 1
        upvote.save()
        print upvote.counter
        return redirect('/dashboard')
    

    和模型...

    class Upvote(models.Model):
        user = models.ForeignKey(User, related_name="upvote_user", null=True)
        comment = models.ForeignKey(Comment, related_name="comment_upvote", null=True)
        counter = models.IntegerField(null=True, blank=False, default=0)
        created_at = models.DateField(null=True)
        class Meta:
            db_table = 'upvote'
    

    views.py 模板的渲染函数..

    def show_topic(request, topic_id):
        print "Showing a topic"
        topic = Topic.objects.get(id=topic_id)
        comments = Comment.objects.all().filter(topic=topic)
        upvotes = Upvote.objects.all()
        ideas = Idea.objects.all()
        downvotes = Downvote.objects.all()
        context = {'upvotes': upvotes, 'ideas': ideas, 'comments': comments, 'topic': topic,'downvotes': downvotes}
        return render(request, 'discussionboard/show.html', context)
    

    【讨论】:

    • 你能告诉我们你的模板渲染函数吗?
    • 嘿 ferdy,刚刚添加了它。
    猜你喜欢
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多