【发布时间】:2015-04-19 16:00:55
【问题描述】:
我正在使用 django allauth 进行用户身份验证,到目前为止,我已经能够像这样显示用户的页面:account/1/trial/,其中 1 是 pk 号,trial 是用户名(唯一),或者类似这个:帐户/1/。在这两种情况下,一切正常,但如果我只想在 url 中显示用户名 (account/trial/),则无法加载登录用户的配置文件 (accounts/profile/) 页面 (404)。可能是我的配置文件功能错误,我该如何纠正它,以便页面能够正常加载,就像在 url 中使用 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))
【问题讨论】:
-
对于初学者,将目标
profile/$移动到 slug 模式匹配上方 -
谢谢,这解决了问题。