为什么django无法识别我的url?
我的用户模型中的URL.py文件出现问题。当我访问localhost:5000/users/时,会看到预期的页面,但当我转到localhost:5000/users/1时,会看到一个页面为什么django无法识别我的url?,django,django-urls,Django,Django Urls,我的用户模型中的URL.py文件出现问题。当我访问localhost:5000/users/时,会看到预期的页面,但当我转到localhost:5000/users/1时,会看到一个页面 Page not found (404) Request Method: GET Request URL: http://127.0.0.1:5000/users/1 Using the URLconf defined in ng_blog.urls, Django tried these UR
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:5000/users/1
Using the URLconf defined in ng_blog.urls, Django tried these URL patterns, in this order:
^users/$
但是我的url.py似乎定义得很恰当
urlpatterns = patterns('',
url(
r'^$',
UserList.as_view(),
name='user_list'
),
url(
r'^(?P<username>\w+)/$',
UserDetail.as_view(),
name='user_detail'
),
)
问题的一部分在于,您已将
用户详细信息
视图定义为仅匹配以/字符结尾的URL,而未在失败的URL中使用。而且(正如其他人所指出的)您的用户名
模式实际上与任何字符都不匹配。但是从尝试的模式列表中,我怀疑您的ng_blog.url
文件中可能存在其他问题。我认为您需要将(?p)
替换为(?p[^/]+)
(或其他适当的模式) 在root url.py中,更改
url(r'^users/$', include('users.urls')),
到
$
表示url模式的结束。这就是问题所在
还有,改变
r'^(?P<username>)/$',
r'^(?P)/$',
到
r'^(?P\w+/$”,
改用r'^(?p\d+)/$试试
d+将确保匹配1个或多个数字。能否显示根url.py文件?另外,在
r'^(?P)/$”中需要一个正则表达式模式,类似于:r'^(?P\w+/$”
我只是想写这个;)+1给你!因此,正则表达式是一个包含URL.py文件的延续?来自文档:“请注意,本例中的正则表达式没有$(字符串结尾匹配字符),但包含尾部斜杠。每当Django遇到include(),它切掉与该点匹配的URL的任何部分,并将剩余字符串发送到包含的URLconf进行进一步处理。您正在请求用户名,在您的示例中,您有/users/1
确保这是您想要的,或者您可能希望将模式更改为r'^(?P\d+/$)
url(r'^users/', include('users.urls')),
r'^(?P<username>)/$',
r'^(?P<username>\w+)/$',