Django URL文件错误

时间:2017-12-12 15:16:51

标签: python django url web

我在Django项目中有以下urls.py文件,我收到一个错误,我认为该错误与网址和路径相关的最新语法有关。

我在url文件中的代码是mysite(最外层目录)中的url.py:

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


urlpatterns = [
    path(r'^$', include('aboutme.urls')),

]

错误讯息为:

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

在名为'aboutme'的实际应用程序(网站文件夹)中,urls.py文件如下所示:

from django.urls import path
from django.conf.urls import url, include 
from .import views #this is the main thing we are going to be doing ....returning views!

urlpatterns = [

        path(r'^$', views.index,name='index'),

]

任何人都可以了解正确的语法或我做错了吗?

更新:

我也回过头来尝试更新主要的mysite的url.py文件,以包含管理命令(以前的工作版本)。代码和结果错误也显示如下:

试过这段代码:

from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include 


urlpatterns = [
    path('admin/', admin.site.urls),
    path(r' ', include('aboutme.urls')),

]

错误

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

1 个答案:

答案 0 :(得分:2)

删除^文件中的$urls.py

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

urlpatterns = [
    path(r'', include('aboutme.urls')),
]

在您的应用urls.py中:

from django.urls import path
from django.conf.urls import url, include 
from .import views #this is the main thing we are going to be doing 

app_name="myappname"
urlpatterns = [
    path(r'', views.index,name='index'),
]

在django 2.0中,如果您使用path(),则不再需要它们。

相关链接:https://code.djangoproject.com/ticket/28691