简体   繁体   中英

Want to display rating stars in django

I am in new in django and i have a model "album" which has 3 fileds title , genre and rating and i am displaying them ia table and i want to display the digit"0" as many time as album.rating and i am using for loop from 0 to album.rating but it is displaying only once ie if the album.rating is 2 then "0" should display only 2 times but in my case it is displaying only 1 time .Please help me.Thanks in advance.

Here is the code of the html -

   {% if albums %}
   {% for album in albums %}
   <tbody>
     <tr>
     <td>{{ album.album_title }}</td>
     <td>{{ album.genre }}</td>

      <!-- rating stars -->
      <td>
      {% for i in album.rating %}
      <option value={{i}}>0</option>
     {% endfor %}
      </td>


      <td>  
      <a href="{% url 'music:edit' album.id %}" class="btn btn-primary btn-sm" role="button">Edit</a>
      </td>
   <td>  

        </td>
        </tr>
       </tbody>

Here is the code of view.py

    def index(request):
    if not request.user.is_authenticated():
    return render(request, 'music/login.html')
else:
    albums = Album.objects.filter(user=request.user)
    paginator = Paginator(albums, 2) # Show 25 contacts per
    page = request.GET.get('page')
    try:
          albums = paginator.page(page)
    except PageNotAnInteger:
           # If page is not an integer, deliver first page.
           albums = paginator.page(1)
    except EmptyPage:
           # If page is out of range (e.g. 9999), deliver last page of results.
           albums = paginator.page(paginator.num_pages)
    song_results = Song.objects.all()
    query = request.GET.get("q")
    if query:
        albums = albums.filter(
            Q(album_title__icontains=query) |
            Q(artist__icontains=query)
        ).distinct()
        song_results = song_results.filter(
            Q(song_title__icontains=query)
        ).distinct()
        return render(request, 'music/index.html', {
            'albums': albums,
            'songs': song_results,
        })
    else:
        return render(request, 'music/index.html', {'albums': albums})

Since you could not get the way to implement, after explaining:

{% for i in album.rating %} is like {% for i in 2 %} in your case, which turns out that, for a single digit, it's going to loop once. use range filter or so on.

I can suggest the quickest way to get it implemented via the answer: Check this

{% if albums %}
    {% for album in albums %}
    <tbody>
        <tr>
            <td>{{ album.album_title }}</td>
            <td>{{ album.genre }}</td>
            <!-- rating stars -->
            <td>
            {% with ''|center:album.rating as range %}
                {% for i in range %}
                    <option value={{i}}>0</option>
                {% endfor %}
            {% endfor %}
            </td>
            <td><a href="{% url 'music:edit' album.id %}" class="btn btn-primary btn-sm" role="button">Edit</a></td>
            <td></td>
        </tr>
    </tbody>
{% endif %}

Humble opinion, please look into DJango template filters and try and check this .

PS: have not evaluated the solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM