【问题标题】:Django url pk + slug page not found找不到 Django url pk + slug 页面
【发布时间】: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 模式匹配上方
  • 谢谢,这解决了问题。

标签: python django url slug


【解决方案1】:

字符串 "profile" 匹配 slug-only 用户名视图的正则表达式,并且由于 Django 按顺序匹配 URL,对 URL "profile/" 的请求将始终转到该视图。简单的解决方案是将配置文件 URL 移到另一个上面。

【讨论】:

  • 谢谢丹尼尔,我应该马上想到的......无论如何感谢您的快速解决方案
猜你喜欢
  • 2017-05-08
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 2020-06-17
  • 1970-01-01
  • 2013-04-02
  • 2014-07-12
相关资源
最近更新 更多