Monday, May 24, 2010

rendering to template help

I have been trying to get some kind of kind response from django for a
few days now and it has been very unkind to me :P

basically i need to create a page that has a video title (link) and an
average rating with the amount of people that rated it in brackets.

so it will look like ... Awesom video 5(3) : where awesom video is
the name 5 is the average rating and 3 people have rated the video.

i can display the videos easily by using a for loop in the template
however i am having much difficulty displaying the average ratings and
count

rating model:


class RatingManager(models.Manager):
def average_rating(self,video):
avg = Rating.objects.filter(id=video.id).aggregate(Avg('rating'))
return avg

def vote_count(self,video):
num = Rating.objects.filter(id=video.id).aggregate(Count('rating'))
return num

class Rating(models.Model):
user = models.ForeignKey(User, unique=False)
vid = models.ForeignKey(Video, unique=False)
rating = models.PositiveIntegerField(help_text="User rating of the
video.")
objects = RatingManager()

def __str__(self):
return str(self.user) + "'s rating: " + str(self.vid)

View:

def index(request):
videos = Video.objects.all()
dict= []
for vid in videos:
ratec =
(Rating.objects.vote_count(vid),Rating.objects.average_rating(vid))
dict.append(ratec)

return render_to_response("index.html",
{'videos':videos,'ratecount':dict})

index.html: (please note ratecount is just trial and error this could
potentially be by all means completely incorrect)

<table><tr><th>Title</th><th>Average Rating (Vote Count)</th></tr>
{% for video in videos %}
<tr><td><a href="{{video.url}}">{{video.title}}</a></
td><td>{{ratecount.0.1}}({{ratecount.0.0}})</td>
{% endfor %}

thanks in advance

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate