Django投票:按投票排序

时间:2012-11-09 11:18:29

标签: python html django

已经有很多关于此的文档,但是还没有能够让它们中的任何一个为我工作。就像标题所暗示的那样,试图获得一组使用Django Voting根据他们的投票数量(从高到低)进行排序的对象。尝试了this和其他几个,但没有任何成果。

使用Django投票,这里是URL Conf& HTML

#urls.py
url(r'^$', 'questions'),
url(r'^(?P<object_id>\d+)/(?P<direction>up|down|clear)/vote/$', 
        vote_on_object, dict(model = Question, template_object_name = 'question',
        template_name = 'qanda/confirm_vote.html', post_vote_redirect = '/home/', allow_xmlhttprequest=True)),

URLconf中的问题会导致问题视图呈现questions.html

#questions.html
{% load voting_tags %}
    {% votes_by_user user on the_question as vote_dict %}
    {% scores_for_objects the_question as score_dict %}
        <div class="question">
            {% if the_question %}
        <ul>
            {% for question in the_question %}
               {% dict_entry_for_item question from vote_dict as vote %}
                {% dict_entry_for_item question from score_dict as score %}
           <div class="votearrowdiv">
           <div class="upvotearrow"></div></a>
            <form class="linkvote" id="linkup{{ question.id }}" action="/home/{{ question.id }}/{% if vote and vote.is_upvote %}clear{% else %}up{% endif %}/vote/" method="POST">
                {% csrf_token %}
                <input type="image" id="linkuparrow{{ question.id }}" src="{{ media_url }}img/aup{% if vote and vote.is_upvote %}mod{% else %}grey{% endif %}.png">
            </form>

            <div class="downvotearrow"></div></a>
            <form class="linkvote" id="linkdown{{ question.id  }}" action="/home/{{ question.id }}/{% if vote and vote.is_downvote %}clear{% else %}down{% endif %}/vote/" method="POST">
                {% csrf_token %}
                <input type="image" id="linkdownarrow{{ question.id  }}" src="{{ media_url }}img/adown{% if vote and vote.is_downvote %}mod{% else %}grey{% endif %}.png">
            </form>
            </div>
            <li>
                <div class="votecounter"><div class="numbercount">
                   <span class="score" id="linkscore{{ question_id }}"
                    title="after {{ score.num_votes|default:0 }} vote{{ score.num_votes|default:0|pluralize }}">
                    {{ score.score|default:0 }}
                   </span>
                </div></div>
                <a href="/home/{{ movie.id }}/{{ question.id }}/">{{ question.question_text }}</a>
        {% endfor %}
{% endif %}

以下是我当前的观点:

#views.py
def questions(request, movie_id):
    p = Movie.objects.get(pk=movie_id)
    k = Question.objects.filter(movie=p).order_by('q_pub_date')
    l = k.reverse()
    return render_to_response('qanda/questions.html', {'movie':p, 'the_question':l}, context_instance = RequestContext(request))

我知道我无法使用&#34;得分&#34;因为它不在模型中。在我看来,我需要更改哪些内容才能正确排序?

编辑:

罗伯特,这里是models.py。试过你的解决方案和一些变化,但我没有对模型的投票属性。看看:

#models.py
class Question(models.Model):
    movie           = models.ForeignKey(Movie, blank=True, null=True)
    question_text   = models.CharField(verbose_name = "Question", max_length = 250)
    question_detail = models.CharField(verbose_name = "Details (Optional)", max_length = 5000, blank = True, null = True)
    q_pub_date      = models.DateTimeField(auto_now_add = True)
    q_author        = models.ForeignKey(User)

有什么见解?

1 个答案:

答案 0 :(得分:1)

如果您发布了model.py,那会很方便,但我会做出一些猜测。

首先,这可能会有所帮助:

#views.py
def questions(request, movie_id):
    p = Movie.objects.get(pk=movie_id)
    k = Question.objects.filter(movie=p).order_by('-q_pub_date')
    ...

(不需要使用反向,可以用-

开始

我猜你的分数可以分类如下:

    k = Question.objects.filter(movie=p).order_by('movie__score', '-q_pub_date')

__(双下划线)将引用相关模型的属性。

我知道这样生活和死亡:https://docs.djangoproject.com/en/dev/topics/db/queries/#related-objects