Django urlpattern“不匹配”

时间:2013-12-18 10:37:27

标签: python django url

我在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'),
)

新闻目录中有views.py,它包含新闻功能。

新闻模块已添加到INSTALLED_APPS。

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

3 个答案:

答案 0 :(得分:3)

您似乎忘记了url()功能:

请改为尝试:

from django.conf.urls import url

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

和news / 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'),
)

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我认为这可能是你的views.py上的问题。这个django docs上的url会告诉你如何发送urls properley

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