NoReverseMatch Django最爱

时间:2017-10-29 16:48:44

标签: python django

我对这个Django的东西很新,我在/ cityinfo /

获得NoReverseMatch

例外值:

Reverse for 'user_favorites' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['cityinfo/(?P<fav_id>[0-9]+)/$']

基本上我要做的就是让所有用户收藏最喜欢的帖子,并在用户点击base.html中最喜欢的导航链接时显示它们

base.html文件

                <li class="#">
                    <a href="{% url 'cityinfo:user_favorites' favorites.id %}">
                        <span class="glyphicon glyphicon-floppy-disk"></span>&nbsp; Favourites
                    </a>
                </li>

urls.py

url(r'^(?P<fav_id>[0-9]+)/$', views.user_favorites, name="user_favorites"),

views.py

def user_favorites(request, fav_id):
if not request.user.is_authenticated():
    return render(request, 'cityinfo/login.html')
else:
    favorites = get_object_or_404(user_favourite_spot, id=fav_id)
    context = {
        "favorites": favorites
    }
    return render(request, 'cityinfo/user_favorites.html', context)

感谢您的帮助

1 个答案:

答案 0 :(得分:4)

模板的上下文中没有fav_id,因此当模板呈现变量时,它会呈现给''

将您的url代码更改为{% url 'cityinfo:user_favorites' favourites.id %}