29

ユーザーがアイテムに投票できる小さなアプリを作成しています。私はDjangoを使用しています(そして初めてです!)。

ユーザーに賛成票のリンクを提示する最良の方法は何だろうと思っています。リンク、ボタン、または何か他のものとして?

私はすでに別のフレームワークを使用してphpでこのようなことを行っていますが、同じ方法でできるかどうかはわかりません。賛成/反対投票の方法を用意してから、クリックするユーザーへのリンクを表示する必要があります。クリックすると、メソッドが実行され、ページが更新されますか?

4

4 に答える 4

38

これが私の解決策の要点です。クリックを処理するためにjQuery/AJAXで画像を使用しています。このサイトの影響を強く受けています。いくつかの作業を使用できるものがいくつかありますが(たとえば、クライアントでのエラー処理-そしてその多くはおそらくリファクタリングされる可能性があります)、コードが役立つことを願っています。

HTML:

        <div class="vote-buttons">
        {% ifequal thisUserUpVote 0 %}
        <img class="vote-up" src = "images/vote-up-off.png" title="Vote this thread UP. (click again to undo)" />
        {% else %}
        <img class="vote-up selected" src = "images/vote-up-on.png" title="Vote this thread UP. (click again to undo)" />
        {% endifequal %}
        {% ifequal thisUserDownVote 0 %}
        <img class="vote-down" src = "images/vote-down-off.png" title="Vote this thread DOWN if it is innapropriate or incorrect. (click again to undo)" />
        {% else %}
        <img class="vote-down selected" src = "images/vote-down-on.png" title="Vote this thread DOWN if it is innapropriate or incorrect. (click again to undo)" />
        {% endifequal %}
        </div> <!-- .votebuttons -->

jQuery:

$(document).ready(function() {

    $('div.vote-buttons img.vote-up').click(function() {

        var id = {{ thread.id }};
        var vote_type = 'up';

        if ($(this).hasClass('selected')) {
            var vote_action = 'recall-vote'
            $.post('/ajax/thread/vote', {id:id, type:vote_type, action:vote_action}, function(response) {
                if (isInt(response)) {
                    $('img.vote-up').removeAttr('src')
                        .attr('src', 'images/vote-up-off.png')
                        .removeClass('selected');
                    $('div.vote-tally span.num').html(response);
                }
            });
        } else {

            var vote_action = 'vote'
            $.post('/ajax/thread/vote', {id:id, type:vote_type, action:vote_action}, function(response) {
                if (isInt(response)) {
                    $('img.vote-up').removeAttr('src')
                        .attr('src', 'images/vote-up-on.png')
                        .addClass('selected');
                    $('div.vote-tally span.num').html(response);
                }
            });
        }
    });

AJAXリクエストを処理するDjangoビュー:

def vote(request):
   thread_id = int(request.POST.get('id'))
   vote_type = request.POST.get('type')
   vote_action = request.POST.get('action')

   thread = get_object_or_404(Thread, pk=thread_id)

   thisUserUpVote = thread.userUpVotes.filter(id = request.user.id).count()
   thisUserDownVote = thread.userDownVotes.filter(id = request.user.id).count()

   if (vote_action == 'vote'):
      if (thisUserUpVote == 0) and (thisUserDownVote == 0):
         if (vote_type == 'up'):
            thread.userUpVotes.add(request.user)
         elif (vote_type == 'down'):
            thread.userDownVotes.add(request.user)
         else:
            return HttpResponse('error-unknown vote type')
      else:
         return HttpResponse('error - already voted', thisUserUpVote, thisUserDownVote)
   elif (vote_action == 'recall-vote'):
      if (vote_type == 'up') and (thisUserUpVote == 1):
         thread.userUpVotes.remove(request.user)
      elif (vote_type == 'down') and (thisUserDownVote ==1):
         thread.userDownVotes.remove(request.user)
      else:
         return HttpResponse('error - unknown vote type or no vote to recall')
   else:
      return HttpResponse('error - bad action')


   num_votes = thread.userUpVotes.count() - thread.userDownVotes.count()

   return HttpResponse(num_votes)

そして、スレッドモデルの関連部分:

class Thread(models.Model):
    # ...
    userUpVotes = models.ManyToManyField(User, blank=True, related_name='threadUpVotes')
    userDownVotes = models.ManyToManyField(User, blank=True, related_name='threadDownVotes')
于 2009-10-07T04:55:55.807 に答える
14

プラグアンドプレイ:

RedditStyleVoting
django-voting を使用して任意のモデルに reddit スタイルの投票を実装する
http://code.google.com/p/django-voting/wiki/RedditStyleVoting

于 2009-10-07T00:57:08.923 に答える
11

何をするにしても、GET ではなく POST によって送信されていることを確認してください。GET 要求でデータベース情報を変更することはできません。

于 2009-10-06T23:18:43.683 に答える
7

リンク、ボタン、または何か他のものとして?

他の何か、画像はどうですか?

クリックすると、メソッドが実行され、ページが更新されますか?

おそらく、ajax を使用してメソッドを呼び出して投票を保存し、何も更新しない方がよいでしょう。

これが私の頭に浮かぶものです。

ここに画像の説明を入力

于 2009-10-06T23:13:58.950 に答える