【问题标题】:The current URL, , didn't match any of these当前 URL 与其中任何一个都不匹配
【发布时间】:2015-03-14 05:52:16
【问题描述】:

我已经在其他 stackoverflow 线程上检查过这个错误,但在我的代码中没有发现任何错误。也许我累了,但我觉得还可以。

website.urls.py:

from django.conf.urls import patterns, include, url
#from django.contrib import admin

urlpatterns = patterns('',
    # Register and Login
    url(r'^inscription\.html$', 'membres.views.register'),

    # List of users
    url(r'^membres', include('membres.urls')),
)

membres.urls.py:

from django.conf.urls import patterns, url

urlpatterns = patterns('membres.views',
    url(r'^/(?P<slug>\d+)\.html$', 'view_user_public')
)

我当然明白了:

Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:

^inscription\.html$
^membres ^/(?P<slug>\d+)\.html$

The current URL, membres/nicolas.html, didn't match any of these.

inscription.html 可以正常工作。在membres.urls.py 文件中,如果我将r'^/(?P&lt;slug&gt;\d+)\.html$ 更改为r'^\.html$,则网址membres.html 可以正常工作并加载视图...

r'^/(?P&lt;slug&gt;\d+)\.html$ 有什么问题?

非常感谢

【问题讨论】:

  • 尝试删除网址开头的^。这意味着“字符串的开头”,它不会匹配(因为字符串以 membres 开头。
  • 为什么每个 URL 都以 .html 结尾?没必要这么做。
  • 因为这样我才能找到漂亮的 URL :)。

标签: python django django-urls


【解决方案1】:

\d+ 将仅匹配数字。 nicolas 不是由数字组成的。

\w+ 就是你要找的东西。

更一般地说,[\w-]+ 用于通常包含连字符的 slug。

【讨论】:

    猜你喜欢
    • 2016-03-17
    • 2017-03-05
    • 2020-11-22
    • 1970-01-01
    • 2011-09-25
    • 2020-04-18
    • 2020-01-02
    相关资源
    最近更新 更多