我想知道每个单独的播放列表中的观看次数,我可以在其中存储阵列中每个观看次数的观看次数,然后在模板中显示它,但在模板中的以下内容中显示该阵列:
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
这里数组被播放列表重复了,我不能在数组的元素之间移动
问题2:我无法确定每人的观看次数
def playlist_view(request,id):
category = get_object_or_404(Category,pk=id)
playlists = Playlist.objects.filter(category=category)
views=list()
for playlist in playlists:
views.append(PlaylistView.objects.filter(playlist=playlist).count())
print(views)
return render(request, 'playlist.html',{'category': category,'playlists': playlists,'views':views})
{% for playlist in playlists %}
{{ views }} :: {{ playlist.name }}
{% endfor %}
def track_view(request,id_category,id_playlist):
category = get_object_or_404(Category, pk=id_category)
playlist= get_object_or_404(Playlist,pk=id_playlist)
if not PlaylistView.objects.filter(playlist=playlist, session=request.session.session_key):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
view = PlaylistView(playlist=playlist, ip=ip, created_dt=datetime.now(), session=request.session.session_key)
view.save()
这是对Array元素的正确访问,但不能同时访问播放列表
{% for view in views %}
{{ view }}
{% endfor %}
{% for playlist in playlists %}
{{ views }} :: {{ playlist.name }}
{% endfor %}
如何在同一循环中访问两个数组
答案 0 :(得分:0)
正如我所看到的,您需要每个播放列表的视图, 在这种情况下,您可以使用setattr()函数将视图计数设置为播放列表对象。但是更好的方法是进行这样的查询
from django.db.models.aggregates import Count
Playlist.objects.filter(category=category).annotate(view_count= Count('related_name'))
注意:相关名称应来自带有播放列表的PlaylistView外键字段。 比您可以像这样访问-
for playlist in playlists:
{{ playlist.view_count }} :: {{ playlist.name }}
对于每个播放列表的播放列表视图,您可以编写如下查询:
playlist = Playlist.objects.filter(category=category).prefetch_related(Prefetch('related_name', PlaylistView.objects.all(), 'my_list'))
然后您可以访问该查询集,例如:
for playlist in playlists:
{{ playlist.view_count }} :: {{ playlist.name }}
for view in playlist.my_list:
{{view}}
答案 1 :(得分:0)
感谢您的回复,但是每个类别都有很多播放列表,每个播放列表都有很多PlaylistView。
我已经成功存储了每个播放列表的PlaylistView:
def track_view(request,id_category,id_playlist):
category = get_object_or_404(Category, pk=id_category)
playlist= get_object_or_404(Playlist,pk=id_playlist)
if not PlaylistView.objects.filter(playlist=playlist, session=request.session.session_key):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
view = PlaylistView(playlist=playlist, ip=ip, created_dt=datetime.now(), session=request.session.session_key)
view.save()
如何在此处(在另一页上)了解每个播放列表的播放列表视图:
def playlist_view(request,id):
category=get_object_or_404(Category,pk=id)
playlists=Playlist.objects.filter(category=category)
for playlist in playlists:
PlaylistView.objects.filter(playlist=playlist).annotate(view_count=Count('playlistviews'))
感谢阿吉安!