【问题标题】:Django urlpattern "didn't match"Django urlpattern“不匹配”
【发布时间】:2013-12-18 10:37:26
【问题描述】:

我的 urls.py 中有以下代码:

urlpatterns = patterns('',
    (r'^news/', include('news.urls')),
)

当我尝试打开时

http://localhost/news 

http://localhost/news/ 

在浏览器 django 中显示 404 页面:

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

^news/

The current URL, , didn't match any of these.

UPD:

新闻/urls.py:

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

urlpatterns = patterns('news.views',
    (r'^$', 'news'),
)

news目录下有views.py,里面有新闻功能。

并将新闻模块添加到 INSTALLED_APPS。

为什么找不到新闻模式?有什么建议吗?

【问题讨论】:

  • 向我们展示您的news/urls.py?
  • URLconf defined in python.urls
  • 我从未听说过 loca。听起来不错(你在这里少了一个 l --> localhost/news)
  • 是的,'loca...' 在我的问题中是错字

标签: python django url


【解决方案1】:

您好像忘记了url() 函数:

试试这个:

from django.conf.urls import url

urlpatterns = patterns('',
    url(r'^news/', include('news.urls')),
)

和新闻/urls.py:

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from django.conf.urls import url

urlpatterns = patterns('news.views',
    url(r'^$', 'news'),
)

【讨论】:

    【解决方案2】:

    您的应用程序在哪个端口启动?当我看到您的尝试http://locahost/news 时,可能是您忘记使用正确的端口。

    尝试访问http://localhost:8000/http://localhost:8000/news

    【讨论】:

    • 我通过 manage.py runfcgi host=127.0.0.1 port=8080 启动了 fastcgi 并添加了 fastcgi_pass 127.0.0.1:8080;到 nginx 虚拟主机文件
    【解决方案3】:

    我认为这可能是您的views.py django docs上的这个网址告诉您如何正确调度网址

    https://docs.djangoproject.com/en/dev/topics/http/urls/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 2015-11-30
      • 2013-10-07
      • 2019-08-07
      • 2014-01-18
      • 2013-07-16
      相关资源
      最近更新 更多