0

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 i a 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 i.e 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})
user3923278
  • 85
  • 1
  • 11
  • Is your html content correct? You have `` without `select` element and also could you provide the full block of `{% if albums %}` to `{% endif %}` ? – Nagaraj Tantri Jun 06 '16 at 05:38
  • please don't ever post it's not working in a question. Instead always post what you expected and what you got – e4c5 Jun 06 '16 at 05:44
  • @e4c5 please refer to my edited post 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 i.e if the album.rating is 2 then "0" should display only 2 times but in my case it is displaying only 1 time – user3923278 Jun 06 '16 at 05:54
  • @NagarajTantri please refer to my edited post 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 i.e if the album.rating is 2 then "0" should display only 2 times but in my case it is displaying only 1 time – user3923278 Jun 06 '16 at 05:54
  • It could be quite possible that the problem is in your data. did you check what album.rating actually contains? – e4c5 Jun 06 '16 at 05:59
  • @user3923278 obviously because: `{% 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. http://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates – Nagaraj Tantri Jun 06 '16 at 06:03
  • @e4c5 in my model i have defined it as rating = models.CharField(max_length=100) and in it i am storing integer values.and while displaying it display the correct value. – user3923278 Jun 06 '16 at 06:04
  • @NagarajTantri thanks for ur advise but i already read that article but i didnot get anything.can u please explain me how to use range filter in my case.please – user3923278 Jun 06 '16 at 06:06

1 Answers1

2

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.

P.S: have not evaluated the solution

Community
  • 1
  • 1
Nagaraj Tantri
  • 5,172
  • 12
  • 54
  • 78