我想创建一个页面,用户可以在其中看到他喜欢的帖子。如何修改以下模板以显示“收藏夹”帖子?
output_directory
这是 .html 文件
{% if (user has any favorite posts %}
show here
{% else %}
User has no favorite posts
{% end if %}
</tr>
这是 view.py 文件
<td>
<form method='POST' action="{% url 'foobar:favourite_post' video.id %}">
{% csrf_token %}
<input type='hidden'>
<button type='submit'>Bookmark</button>
</form>
</td>
这是 models.py 文件
def favourite_post(request, fav_id):
video = get_object_or_404(Video, id=fav_id)
if request.method == 'POST':
video.favourite.add(request.user)
return redirect('/foobar/%s' % fav_id)
答案 0 :(得分:1)
您所需的只是for-empty
{% for video in user.fav_videos.all %}
<td>{{ video.name }}</td>
{% empty %}
<td >No Bookmarked Videos</td>
{% endfor %}