2013-02-07 142 views
1

我正在通过https://docs.djangoproject.com/en/1.4/intro/tutorial02/工作。在django tututorial页面未找到(404)

改变urls.py到

from django.conf.urls import patterns, include, url 

# Uncomment the next two lines to enable the admin: 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    # Examples: 
    # url(r'^$', 'mysite.views.home', name='home'), 
    # url(r'^mysite/', include('mysite.foo.urls')), 

    # Uncomment the admin/doc line below to enable admin documentation: 
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 

    # Uncomment the next line to enable the admin: 
     url(r'^admin/', include(admin.site.urls)), 
) 

我拿到后,当我开始的runserver如下:

404 error 

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: 
^admin/ 
The current URL, , didn't match any of these. 

有什么明显的是我做错了吗?

由于提前,

比尔

+0

看起来好像你试图击中一个空的url,并且你还没有定义一个空的url去的地方。我假设你的意思不是当你“启动runserver”,而是你运行'manage.py runserver',然后在浏览器中打localhost:8000并得到这个错误? – Colleen

回答

2

您没有定义的基本URL。你需要这样的东西 -

urlpatterns = patterns('', 

    # ... 
    url(r'^$', HomeView.as_view()) 

) 

你应该能够看到你的站点 - 本地主机:8000 /管理/(假设你有python manage.py runserver运行你的开发服务器)。

Django会检查您在url conf文件中定义的所有网址,并查找与您在浏览器中输入的网址相匹配的网址。如果它找到一个匹配的URL,那么它会提供URL相应视图(上面代码中的HomeView)返回的http响应。 urls.py文件将url与视图匹配。视图返回http响应。

查看您收到的错误消息(以及您从url.py文件中包含的代码),您可以看到应用中只定义了一个url - admin/。试图在任何其他网址获取页面将失败。

欲了解更多信息,看看docs for django's URL Dispatcher

+0

感谢您的详细信息 – user61629

+0

没问题。乐意效劳! –