Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get django-star-ratings to display to template

New to Django, feel like I'm close to figuring out where I'm going wrong here. I have been trying to pass the context to my template to no avail. In models I have:

class Rate(models.Model):
    name = models.CharField(max_length = 140)
    ratings =  GenericRelation(Rating, related_query_name= 'object_list')

    def __str__(self):
        return self.id

And in views,

def RateList(request):
    queryset = Rate.objects.filter(ratings__isnull=False).order_by('ratings__average')
    context= {
        "object_list": queryset,
        "title": "List"
    }
    return render(request, 'UploadApp/upload.html', context)

and lastly, in my template I've put {% ratings object_list %} into the HTML as per the documentation. Not sure if I'm just overlooking some small detail, but I'm getting an 'str' object has no attribute 'meta' error when I try and load the page. Any help is appreciated, I'm at the hair pulling stage

like image 293
Adam Yui Avatar asked Nov 08 '22 02:11

Adam Yui


1 Answers

In method __str__ you are returning self.id, but there is no field named id in your model, So update your code like this :

class Rate(models.Model):
    name = models.CharField(max_length = 140)
    ratings =  GenericRelation(Rating, related_query_name= 'object_list')

    def __str__(self):
        return self.name

One more thing, The object_list you are passing in context is a list of Rate model objects :

{% ratings object_list %}

This is the wrong method to show/iterate your data.

What you actually need in your template is:

{% for item in object_list %}

    {{ item.name }} and {{ item.ratings }}

{% endfor %}
like image 193
Prakhar Trivedi Avatar answered Nov 14 '22 23:11

Prakhar Trivedi