请求的用户个人资料显示在两个网址上

时间:2018-08-24 03:17:04

标签: django

对于已登录用户,个人资料网址为

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

对于个人资料视图,URL为

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

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

views.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,否则显示其他配置文件,但现在仍可以显示其显示的所有配置文件。

0 个答案:

没有答案