下面是官方文档的内容,如果在urls.py中使用到正则匹配路径(^$)的时候,就需要使用re_path,而不能使用path,不然页面会显示404错误,
如果未用到正则,那么使用path即可。
re_path()
?
re_path
(route, view, kwargs=None, name=None)?
Returns an element for inclusion in urlpatterns
. For example:
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即可。
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‘)), ... ]
原文地址:https://www.cnblogs.com/lianxuebin/p/9275278.html
时间: 2024-11-01 02:40:38