Django - 评级模型示例DetailView模板 [英] Django - Rating Model Example DetailView Template
问题描述
这是上下文:
我有用户,视频,主题,标准和评分
Here is the context: I have users, videos, topics, criterias and ratings
- 视频有一个主题
- 主题具有标准
- 用户可以为给定的主题创建视频
- 用户可以为有关主题给出的标准评价一个视频。
- A video has a topic
- A topic has criterias
- A user can create a video for a given topic
- A user can rate a video on the criterias given for the concerned topic.
这是我的模型为这个目的:
Here is my model for that purpose:
RATE_CHOICES = zip( range(1,5), range(1,5) )
class VideoCrit(models.Model):
"""Criteria to rate videos on.
Can be multiple for each Topic of Video"""
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Meta:
verbose_name = 'Video Criteria'
class VideoTopic(models.Model):
name = models.CharField(max_length=50)
descr = models.TextField(blank=True, null=True)
crits = models.ManyToManyField(VideoCrit,
help_text='Criterias to rate the videos',
blank=True, null=True,
)
def __unicode__(self):
return self.name
class Meta:
verbose_name = 'Video Topic'
class VideoFile(models.Model):
"""Uploadable by users to be rated and commented"""
name = models.CharField(max_length=50)
descr = models.TextField(blank=True, null=True)
file = models.FileField(upload_to='videos')
topic = models.ForeignKey(VideoTopic)
def __unicode__(self):
return self.name
class Meta:
verbose_name = 'Chatter Tube'
class VideoRate(models.Model):
"""Users can Rate each Video on the criterias defined for the topic"""
user = models.ForeignKey(User)
video = models.ForeignKey(VideoFile)
crit = models.ForeignKey(VideoCrit)
rate = models.DecimalField(max_digits=2, decimal_places=1, choices=RATE_CHOICES)
class Meta:
unique_together = (('user', 'video', 'crit'),)
verbose_name = 'Tube Rating'
可以吗?
如果是,从给定VideoFile的模板(基于VideoFile类的DetailView)中,以下是模板的有趣部分
If yes, from a template (DetailView based on the VideoFile class) for a given VideoFile, here is the interesting part of the template
<div id="rating">
<ul>
{% for crit in videofile.topic.crits.all %}
<li>
<div class="rateit"
crit-id="{{ crit.id }}"></div>
{{ crit.name }}
</li>
{% endfor %}
</ul>
</div>
URLconf&查看
URLconf & View
#urlconf
#...
(r'viewtube/(?P<pk>\d+)$', VideoFileDetailView.as_view()),
#...
#view
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=11)
return context
如何访问当前记录的评分用户为当前视频?
How can I access to the ratings of the current logged user for the current video ?
推荐答案
更新:获取当前视频的当前登录用户的所有评分
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>
以前:
你应该能够访问登录用户的评分,使用这样的效果:
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 %}
这篇关于Django - 评级模型示例DetailView模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!