title: Simple vote button in Django
body: <p>I'm an absolute beginner to django (and programming in general), I've tried the django Polls tutorial and all went well, however I am trying to get a minimal voting button to work. Basically I have set up a database with two columns, my <code>models.py</code> looks like this</p>
  
  <pre><code>from django.db import models
  
  
  class Imgurl(models.Model):
  
      urlvar = models.URLField(max_length=200)# a URL linking to an image
      urlvote = models.IntegerField(default=0)# my intended vote count
  
      def __unicode__(self):
          return self.urlvar
  </code></pre>
  
  <p>I have made an input box where I can copy and paste an image url, this image then displays on a separate page(this works fine). What I want is to have a voting button next to each displayed image, where a user can click on the button (I am trying to use a submit button) and the number of votes will increase in the db(no redirecting to a new page, or anything fancy). </p>
  
  <p>I think this is a trivial question and I am trying to learn the basics of <code>POST</code> and database handling in django (also, I have read the relevant chapters in the djangobook... maybe I'm just a little slow?)</p>
  
  <p>my views looks like this</p>
  
  <pre><code>def urlvotes(request):
  
      if request.method=='POST':
          if 'voteup' in request.POST:
              v=Imgurl(forloop.counter)
              v.urlvote +=1
          else:
              pass
          votetotal=v.urlvote # attempt to give the template some kind of context
      return render_to_response('display.html', {'votetotal':votetotal}, context_instance=RequestContext(request))
  </code></pre>
  
  <p>and my template looks like this:</p>
  
  <pre><code>{% extends "base.html" %}
  
  {% block head %}Image display{% endblock %}
  {% block content1 %}
  
  &lt;a href="http://127.0.0.1:8000/home/"&gt;Home&lt;/a&gt;
  {% if links %}
          &lt;ul&gt;
          {% for link in links %}
              &lt;li&gt;&lt;img src="{{ link }}"&gt;&lt;/li&gt;
  
              &lt;li&gt;&lt;form action="{% url urlvotes %}" method="post"&gt; 
              {% csrf_token %}
              &lt;input type="submit" name="voteup" value='vote'/&gt;&lt;/p&gt;
              &lt;p&gt;{{votetotal}}&lt;/p&gt;
              &lt;/form&gt;&lt;/li&gt;
  
          {% endfor %}
  
          &lt;/ul&gt;
  {% else %}
  &lt;p&gt;No uploads.&lt;/p&gt;
  {% endif %}
  {% endblock %}
  </code></pre>
  
  <p>when I run this, as is, I get a csrf verification failed error</p>
  
  <p>Any help would be greatly appreciated</p>