Django - Rating Model Example DetailView Template

北战南征 提交于 2019-12-04 21:46:34

UPDATE: To get all the ratings for the currently logged in user for the current video

# in Views.py 

video = VideoFile.objects.get(pk=video_id) #video_id is parameter sent from url
user_ratings = VideoRate.objects.filter(user=request.user).filter(video=video)

# in template
<ul>
{% for rating in user_ratings %}
    <li>{{ rating.crit.name }}: {{ rating.rate }}</li>
{% endfor %}
</ul>

PREVIOUSLY:

You should be able to access the ratings of the logged in user using something to this effect:

user.videorate_set.all

You can then display all of the ratings for a given user in your template as follows:

{% for rating in user.videorate_set.all %}
    {{ rating.video }} {{ ratings.rate }}
{% endfor %}
Pierre de LESPINAY

Django - Generic View Subclassed - url Parameters gave me the answer. I have to add the rates pre filtered to the context for the template.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!