Django新的URL不起作用

时间:2017-10-16 23:54:30

标签: python django

我正在尝试访问网址http://localhost:8000/calc,但它给了我这个错误:

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

    ^admin/
    ^$ [name='home']
    ^calc/

The current path, calc, didn't match any of these.

这是我目前对mysite URL和secondapp URL的所有内容:

 # mysite/urls.py

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'',include('firstapp.urls')),
    url(r'^calc/',include('secondapp.urls')),
]

 # secondapp/urls.py

from django.conf.urls import url
from secondapp.views import CalcPage

urlpatterns = [
    url(r'calc/', CalcPage.as_view(), name='calc'),
]

2 个答案:

答案 0 :(得分:2)

根据你在mysite.url和secondapp.url中声明的URL配置,你链接的将是

localhost:8000/calc/calc/

IMO会让人困惑,如果我是正确的,你想要的网址是

localhost:8000/calc/

为此,您必须将您在secondapp.url中定义的网址更改为

from django.conf.urls import url
from secondapp.views import CalcPage

urlpatterns = [
    url(r'$', CalcPage.as_view(), name='calc'),
]

这样可以在

上访问ClacPage
localhost:8000/calc/

在定义网址时,请参阅/非常重要。

答案 1 :(得分:0)

在urls.py中使用以下reg-ex

r'^calc/$'