下面是官方文檔的內容,若是在urls.py中使用到正則匹配路徑(^$)的時候,就須要使用re_path,而不能使用path,否則頁面會顯示404錯誤,
若是未用到正則,那麼使用path便可。web
re_path()
¶re_path
(
route,
view,
kwargs=None,
name=None)
¶
Returns an element for inclusion in urlpatterns
. For example:django
from django.urls import include, re_path urlpatterns = [ re_path(r'^index/$', views.index, name='index'), re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio'), re_path(r'^weblog/', include('blog.urls')), ... ]
下面是沒有使用正則匹配路徑,使用path便可。this
from django.urls import include, path urlpatterns = [ path('index/', views.index, name='main-view'), path('bio/<username>/', views.bio, name='bio'), path('articles/<slug:title>/', views.article, name='article-detail'), path('articles/<slug:title>/<int:section>/', views.section, name='article-section'), path('weblog/', include('blog.urls')), ... ]