首页 > 解决方案 > 请求的用户配置文件出现在两个 url

问题描述

对于登录用户,个人资料 url 是

path('profile/', profile, name='profile'),

对于个人资料视图,网址是

re_path(r'^profile/(?P<pk>\d+)/$',UserProfileView,name='my_profile'),

但是当我们尝试通过 '/profile/user_pk' 访问登录用户时,它与两个 url 中存在的一个配置文件冲突

视图.py

@login_required
def profile(request):
    args={'user':request.user}
    return render(request,'account/profile.html',args)

def UserProfileView(request,pk):
    if pk==request.user.pk:
        raise(Http404)
    else:
        user = get_object_or_404(User,pk=pk)
    return render(request,'account/profile.html',{'user':user})

如果请求的 pk 与配置文件 pk 相同,那么它会引发 Http 否则显示其他配置文件,但现在仍在工作,仍然显示所有配置文件。

标签: django

解决方案


推荐阅读