title: Django - Simple custom template tag example
body: <p>I have <em>users</em>, <em>videos</em>, <em>topics</em>, <em>criterias</em> and <em>ratings</em></p>
  
  <ul>
  <li>A <em>video</em> has a <em>topic</em></li>
  <li>A <em>topic</em> has <em>criterias</em></li>
  <li>A <em>user</em> can create a <em>video</em> for a given <em>topic</em></li>
  <li>A <em>user</em> can rate a <em>video</em> on each <em>criterias</em> given for the concerned <em>topic</em>.</li>
  </ul>
  
  <p>You can see my original post <a href="https://stackoverflow.com/questions/6388259/django-rating-model-example-detailview-template">Django - Rating Model Example DetailView Template</a> to get details on the model used</p>
  
  <p>I have extended a <code>DetailView</code> template based on the <em>video</em> model to put the list of <em>ratings</em> for the selected <em>video</em> for a given <em>user</em> as extra context.</p>
  
  <pre><code>class VideoFileDetailView(DetailView):
    model = VideoFile
  
    def get_context_data(self, **kwargs):
      context = super(VideoFileDetailView, self).get_context_data(**kwargs)
      context['rates'] = VideoRate.objects.filter(video=self.object, user=self.request.user)
      return context
  </code></pre>
  
  <p>In the template pointed by the <code>DetailView</code>, I'd like to list the <em>criterias</em> of the <em>video</em>, and for each <em>criteria</em> display the current <strong>rating value</strong> form the <em>user</em>.</p>
  
  <pre><code>&lt;div id="rating"&gt;
    &lt;ul&gt;
  {% for crit in videofile.topic.crits.all %}
      &lt;li&gt;
    {% for rate in rates %}
      {% if rate.crit.id == crit.id %}
        {{ rate.rate }}
      {% endif %}
    {% endfor %}
        &lt;div class="rateit"
          data-rateit-value="{# The rating value #}"
          data-rateit-ispreset="true"
          crit-id="{{ crit.id }}"&gt;&lt;/div&gt;
        {{ crit }}
      &lt;/li&gt;
  {% endfor %}
    &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>(<a href="http://www.radioactivethinking.com/rateit/example/example.htm" rel="noreferrer">rateit</a> is a jquery plugin that I use to draw pretty stars rating controls)</p>
  
  <p>Actually I get my rating values here within the 2nd <code>for</code> but I'm sure there is a better way to do that. In fact, I'm still not sure about my <a href="https://stackoverflow.com/questions/6388259/django-rating-model-example-detailview-template">model</a> correctness.</p>
  
  <p>Finally I'd like to replace <code>{# The rating value #}</code> by the rating value from rate for the current crit (in the loop). How can I do that ?</p>