【问题标题】:Variables and Django URLConf变量和 Django URLConf
【发布时间】:2011-05-26 21:37:07
【问题描述】:

我无法从我的 URL 中提取字符串。这就是我得到的......它一直在 404ing。

urls.py:

urlpatterns = patterns('',
    (r'^user/(?P<username>\w{0,50})/$', profile,),
)

views.py:

  def profile(request, username):
            ... 
      return ...

看到什么明显的东西了吗?需要更多?任何帮助表示赞赏。

【问题讨论】:

  • 你在 URL 中给出的字符串是什么?
  • domain/accounts/user/foobar(这个 urls.py 在 /accounts/ 中)
  • 你是在末尾添加斜线吗?
  • 我可以通过任何方式,它仍然是 404s。
  • 啊哈。问题不在这里。它在我的主 urlconf 上面,我有一个终止 $。感谢你们的聪明才智。

标签: python regex django urlconf


【解决方案1】:

您是否在 URL 文件的顶部导入了视图模块?

from views import profile

urlpatterns = patterns('',
    (r'^user/(?P<username>\w{0,50})/$', profile), 
    # also removed trailing comma after profile
)

# alternative

urlpatterns = patterns('',
    (r'^user/(?P<username>\w{0,50})/$', 'views.profile'), 
)

您的设置文件中有 DEBUG = True 吗?这将有助于发现您应该向我们展示的堆栈跟踪错误。

【讨论】:

    【解决方案2】:

    我通常在 url 模式的末尾添加一个 /?$

    这是一个常见错误,某些浏览器添加或不添加尾随'/'。

    【讨论】:

      猜你喜欢
      • 2013-08-11
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 2013-06-01
      • 2012-03-17
      • 1970-01-01
      • 2012-03-08
      相关资源
      最近更新 更多