我对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)


感谢你的帮助

最佳答案

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

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

07-24 21:13