title: Django - How do you get the corresponding model object in a for loop in html?
body: <p>I am trying to create a simple django website where any user can rate and create posts. As displayed in this django tutorial (<a href="https://docs.djangoproject.com/en/1.7/topics/templates/" rel="nofollow noreferrer">https://docs.djangoproject.com/en/1.7/topics/templates/</a>), you can display all the model objects in html using a for loop. In my case, each object is going to be displayed with a Like and a Dislike button, so people can rate the post. My problem is: How do I know which object belongs to which like/dislike button so that the corresponding model field can be changed for that particular object? Thank You for answers!</p>
  
  <p>models.py</p>
  
  <pre><code>from django.db import models
  
  # Create your models here.
  
  class Post(models.Model):
      post_text = models.CharField(max_length=500)
      pub_date = models.DateTimeField("date published")
      likes = models.IntegerField(default=0)
      dislikes = models.IntegerField(default=0)
      def __str__(self):
          return self.post_text
  </code></pre>
  
  <p>index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;AllPosts&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  {% if post_list %}
  &lt;ul&gt;
      {% for post in post_list %}
      &lt;li&gt;{{post.post_text}}&lt;/li&gt;
      &lt;p&gt;This post has {{post.likes}} likes and {{post.dislikes}} dislikes.&lt;/p&gt;
      &lt;br&gt;Leave a &lt;button type="button" method="LIKE"&gt;Like&lt;/button&gt; or a &lt;button type="button" method="DISLIKE"&gt;Dislike&lt;/button&gt;!&lt;/p&gt;
      {% endfor %}
  
  &lt;/ul&gt;
  &lt;h2&gt;If you want to create a post yourself, &lt;a href="{% url 'create' %}"&gt;click here.&lt;/a&gt;&lt;/h2&gt;
  {% else %}
  &lt;h1&gt;There are no posts yet...&lt;/h1&gt;
  {% endif %}
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>from django.http import HttpResponse
  from django.template import loader
  from django.shortcuts import render
  from django.utils import timezone
  from .models import Post
  
  
  # Create your views here.
  
  def index(request):
  
      post_list = Post.objects.order_by('-pub_date')
      template = loader.get_template('post/index.html')
      context = {'post_list': post_list, }
      #if request.method == "LIKE":
      #    post = Post.objects.get(id=) How do I find out the id?
       #   return HttpResponse(template.render(context, request))
      #else:
      return HttpResponse(template.render(context, request))
  
  
  def create(request):
      template = 'post/create.html'
      if request.method == 'POST':
          post = Post()
          post.post_text = request.POST.get("post_text")
          post.pub_date = timezone.now()
          post.save()
  
          return render(request, template)
  
      else:
  
          return render(request, template)
  </code></pre>