想在 Django 中显示评级星
Want to display rating stars in django
我是 django 的新手,我有一个模型 "album",它有 3 个文件的标题、流派和评级,我在 table 中显示它们,我想显示数字“0 “与 album.rating 一样多的次数,我正在使用从 0 到 album.rating 的循环,但它只显示一次,即如果 album.rating 是 2,那么“0”应该只显示 2 次,但是在我的情况下它只显示 1 次。请提前帮助 me.Thanks。
这里是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>
这里是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})
由于无法获取实现方式,说明后:
{% for i in album.rating %}
就像 {% for i in 2 %}
在你的情况下,事实证明,对于一个数字,它会循环一次。使用范围过滤器等等。
我可以通过答案建议实现它的最快方法: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 %}
拙见,请查看DJango模板过滤器并尝试检查
this.
P.S: 尚未评估解决方案
我是 django 的新手,我有一个模型 "album",它有 3 个文件的标题、流派和评级,我在 table 中显示它们,我想显示数字“0 “与 album.rating 一样多的次数,我正在使用从 0 到 album.rating 的循环,但它只显示一次,即如果 album.rating 是 2,那么“0”应该只显示 2 次,但是在我的情况下它只显示 1 次。请提前帮助 me.Thanks。
这里是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>
这里是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})
由于无法获取实现方式,说明后:
{% for i in album.rating %}
就像 {% for i in 2 %}
在你的情况下,事实证明,对于一个数字,它会循环一次。使用范围过滤器等等。
我可以通过答案建议实现它的最快方法: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 %}
拙见,请查看DJango模板过滤器并尝试检查 this.
P.S: 尚未评估解决方案