Voting variable passing to js - I do not use form, POST or django-vote

advertisements

I have a simple entry and display system for short texts on a single web page. The text input is just an input field with a GET in views. I'd like a very basic up or down voting system on the page for the latest entries. I can get the latest entries into a variable...

Some people online have hinted that a variable can be passed to the js, and this is my first port of call before anything further.

I have not used forms or POST yet, as it seems to include urls and code unnecessary to my basic needs. Can anyone advise me on how I can pass my variable to js?

Here's some snippets of code;

template

<script type="text/javascript">
{% for entry in voting_entry %}
    var votingEntry="{{entry.id}}"
    {% endfor %}
</script>

<script type="text/javascript" src="{{STATIC_URL}}vote.js"></script>

vote.js

$(document).ready(function() {

$("#votingEntry").bind("keypress", function(e) {

    if (e.keyCode == 38) {
        var vote = $("#votingEntry").val();
        var args = {'vote':vote};
        $.get("/vote/", args).done(function(data) {
        location.reload();
      });
    }
  });

views

def index(request):
  context = {
  'latest_entry_list': Entry.objects.order_by('-pub_date')[:10],
  'high_entry_list': Entry.objects.order_by('-score')[:10],
  'low_entry_list': Entry.objects.order_by('score')[:10],
  'voting_entry': Entry.objects.order_by('-pub_date')[:1],
  }
   return render(request, 'entries/index.html', context);

*snip*

def vote(request):
  voting_id = request.GET.get('vote')
  v = Entry.objects.get(pk='voting_id')
  v.score +=1
  v.save()
  return HttpResponse('votedone')


It's not very clear what you are trying to do. In the first snippet you repeatedly set the same variable, votingEntry, to a string containing an ID from a list. Since you set the same variable each time, the var will end up with only the last value from the list.

Additionally, in order to set a global variable outside a function in JS, you need the var keyword.

However, I'm not quite sure why you are doing any of this, because you're then not using the votingEntry variable anywhere else in your JS. You need to provide more detail about what you are hoping to achieve.