django2.0关于path匹配路径页面刷新不出来的问题

下面是官方文档的内容,如果在urls.py中使用到正则匹配路径(^$)的时候,就需要使用re_path,而不能使用path,不然页面会显示404错误,

如果未用到正则,那么使用path即可。

re_path()

re_path(routeviewkwargs=Nonename=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')),
    ...
]
posted @ 2018-07-06 19:30  East~L  阅读(327)  评论(0编辑  收藏  举报