一、出错内容:The current path, index/
, didn't match any of these.
Python 3.6.3,Django3.0.6,Django项目的目录结构如下:
①涉及代码:test1.urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('booktest.urls')),
]
②涉及代码:booktest.urls.py
from django.urls import path
from . import views
urlpatterns = [
path(r'^index/$', views.index)
]
③涉及代码:booktest.views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('hello world')
报错信息如下:
二、修改方法:修改booktest.urls.py
法一:path不需要开始(^)和结束($)符
from django.urls import path
from . import views
urlpatterns = [
path('index/', views.index)
]
法二:path改用re_path
from django.urls import path, re_path
from . import views
urlpatterns = [
re_path(r'^index/$', views.index)
]
三、其他
如果有人在开发的过程中碰到:ValueError: attempted relative import beyond top-level package错误,可以看一下https://python3-cookbook.readthedocs.io/zh_CN/latest/c10/p03_import_submodules_by_relative_names.html