如何在Django中收藏

时间:2017-10-26 15:33:15

标签: python django

我正在尝试创建一个收藏夹按钮,用户只需单击该按钮并为用户收藏了spot_information,也可以删除收藏夹。我想知道为什么当我点击收藏夹按钮时,它会加载一个带有"({'成功':True})"的白页。在最顶端。

我也在学习Bucky Roberts GitHub和YouTube频道"新波士顿",所以我有点像初学者

views.py

def favorite(request, spot_id):
spot = get_object_or_404(spot_information, pk=spot_id)
try:
    if spot.a_favourite:
        spot.a_favourite = False
    else:
        spot.a_favourite = True
    spot.save()
except (KeyError, spot.DoesNotExist):
    return JsonResponse({'success': False})
else:
    return JsonResponse({'success': True})

urls.py

    # favorite
url(r'^(?P<spot_id>[0-9]+)/favorite/$', views.favorite, name='favorite'),

我的索引HTML代码

{% for spotinformation in all_spot_information %}
...
<a href="{% url 'cityinfo:favorite' spotinformation.id %}" class="btn-favorite">
    <button type="button" class="btn btn-primary">
        <span class="glyphicon glyphicon-star-empty" {% if spotinformation.afavorite %}active{% endif %}></span>&nbsp; Favorite
    </button>
</a>
...
{% else %}
{% endif %}

感谢您的帮助。

0 个答案:

没有答案