Django urlpattern “didn't match”

2019-09-18 17:05发布

I have the following code in my urls.py:

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

When I try to open

http://localhost/news 

or

http://localhost/news/ 

in the browser django shows me 404 page:

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:

news/urls.py:

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

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

There is views.py in news directory and it contains news function.

And news module is added to INSTALLED_APPS.

Why it cannot find news pattern? Any suggestions?

3条回答
贪生不怕死
2楼-- · 2019-09-18 17:14

I think it may be a problem with your views.py this url on the django docs tells you how to dispatch the urls properley

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

查看更多
趁早两清
3楼-- · 2019-09-18 17:17

In which port your application launches? When I see your tries http://locahost/news it might be that you forgot to use the right port.

Try to visit http://localhost:8000/ and http://localhost:8000/news.

查看更多
爷的心禁止访问
4楼-- · 2019-09-18 17:33

Looks like you forgot the url() function:

Try this instead:

from django.conf.urls import url

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

and 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'),
)
查看更多
登录 后发表回答