webroot上的django urlconf 404

时间:2015-10-30 20:07:08

标签: python regex django

  • Django:1.8.5
  • Python:3.5

我在webroot上获得了404。我不知道我的urlconf有什么问题。

目录结构:

myproject        // from django-admin startproject
  myproject      // ''                          ''
    __init__.py
    settings.py
    urls.py
    views.py
  myapp          // from manage.py startapp
    migrations
    __init__.py
    admin.py
    models.py
    tests.py
    urls.py
    views.py

在myproject中> urls.py:

from django.conf.urls import include, url, patterns
from myapp import urls as app_urls


urlpatterns = patterns('',
    url(r'^/$', include(app_urls)),
)

在myapp> urls.py:

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

urlpatterns = patterns('',
    url(r'^/$', 'index'),
)

我运行django开发服务器并在索引页面上获得404,即使我已经定义了空的正则表达式。

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

来自全新安装的django的urls.conf中的文档:

Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))

将两个urls.py文件中的正则表达式更改为r'^'似乎有效,但出现新的'Could not import 'index'.错误。

2 个答案:

答案 0 :(得分:1)

使用$时,请勿使用include。将其更改为:

# myproject/urls.py

from django.conf.urls import include, url, patterns
from myapp import urls as app_urls


urlpatterns = patterns('',
    url(r'^', include(app_urls)),
)

# myapp/urls.py

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

urlpatterns = patterns('',
    url(r'^$', 'index'),
)

答案 1 :(得分:0)

根网址为^$,而不是^/$

另外,当你从另一个urlconf中包含一个urlconf时,它会同时使用两个模式;它并没有忘记原始模式。所以你的应用程序真的在寻找我不确定可以匹配的网址^/$^/$