0

i am stuck on this issue for a while.

i have a location in database which can be rated by user using star rating. Then i will save that rate into database. e.g. 1, or 2 till 5 since it is a 5-star-rating system. okay, this is fine.

but now i cannot render that number back to stars again, i want to show all locations which are in DB with their own rated stars. object.rated = 5

my vision for my views.py:

all_locations_from_db = Location.objects.all()
return render_to_response('resultpage.html',{'all':all_locations_from_db},context_instance=RequestContext(request))

and this would be my html:

{% if all %}
{% for every in all %}
    {{ how can i render that number back to 5-star form again?? }}
{% endfor %}
{% endif %}

this is my problem now. can someone please help me? i didnot do anything yet, cos i dont know how to do it..

thanks a lot

doniyor
  • 36,596
  • 57
  • 175
  • 260

2 Answers2

2

If I understand correctly, you are looking for how to create radio button based rating and how to checked the right one.

If so:

{% if all %}
{% for every in all %}
    {% for i in "12345" %}
        {% if forloop.counter == every.rated %}
           <input type="radio" name="rate{{forloop.counter}}" value="{{forloop.counter}}" checked>{{forloop.counter}}
        {% else %}
           <input type="radio" name="rate{{forloop.counter}}" value="{{forloop.counter}}">{{forloop.counter}}
        {% endif %}
    {% endfor %}
{% endfor %}
{% endif %}

looping method based on Numeric for loop in Django templates

Community
  • 1
  • 1
lucemia
  • 6,349
  • 5
  • 42
  • 75
  • It is loop over a string! so I use forloop.counter instead of using "i" directly while doing the comparison. See http://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates – lucemia Mar 18 '13 at 01:07
0

you need to fetch the rating along with locations from the DB. Then iterate this list in the for loop and show the rating for each item in your page.

pravat
  • 465
  • 3
  • 11
  • this is what i already know and do even in my question. my problem is how to show that 5.star-rated number again in star-forms – doniyor Mar 18 '13 at 00:56