找不到Django url pk + slug页面

时间:2015-02-18 15:26:02

标签: python django url slug

我正在使用django allauth进行用户身份验证,到目前为止,我已经能够显示这样的用户页面:account / 1 / trial /,其中1是pk编号,trial是用户名(唯一),或者像这个:帐号/ 1 /。在这两种情况下,一切正常,但如果我只想在网址中显示用户名(帐户/试用/)而不是我无法加载登录用户的个人资料(accounts / profile /)页面(404)。可能我的个人资料功能有误,如何更正它以便页面正常加载,就好像在网址中使用pk一样。

网址:

(r"^(?P<pk>\d+)/(?P<slug>[\w.@+-]+)/$", DetailView.as_view(context_object_name='detail',slug_field = "username",model=User,template_name='account/user_detail.html'), name='detail_view'), #if I use this url the page loads correclty

(r"^(?P<pk>\d+)/$", DetailView.as_view(context_object_name='detail',slug_field = "username",model=User,template_name='account/user_detail.html'), name='detail_view'), # also with this url it works

(r"^(?P<slug>[\w.@+-]+)/$", DetailView.as_view(context_object_name='detail',slug_field = "username",model=User,template_name='account/user_detail.html'), name='detail_view'), #if I use only the slug the page does not load.

(r"^profile/$", views.profile, name="profile_view"), #this is the profile page url

和个人资料页面视图:

def profile(request):
    return render_to_response("account/profile.html",locals(),context_instance=RequestContext(request))

1 个答案:

答案 0 :(得分:0)

字符串&#34;个人资料&#34;匹配仅用于slug的用户名视图的正则表达式,并且由于Django按顺序匹配URL,因此请求URL&#34; profile /&#34;总是会转向那个观点。简单的解决方案是将配置文件URL移到另一个上面。

相关问题