so moving from Django 1.9 to Django 2 is not so smooth for me. I have struck in the URL patterns.
Django 2.0 uses path
instead of URL, How to convert these URL patterns to Django 2.0 compatible?
url(r'^post/(?<pk>\\d+)$',)views.PostDetailView.as_view(), name ='post_detail'),
url('account/login/', views.login, name ='login')
Thanks
By using path()
from django.urls import path
urlpatterns = [
path('post/<int:pk>/', views.PostDetailView.as_view(), name='post_detail'),
path('account/login/', views.login, name='login')
]
You can use re_path()
which behave same as url()
does
from django.urls import re_path
urlpatterns = [
(r'^post/(?<pk>\d+)$', views.PostDetailView.as_view(), name='post_detail'),
(r'account/login/', views.login, name='login')
]
From the doc, ( What's new in Django 2.0 )
The
django.conf.urls.url()
function from previous versions is now available asdjango.urls.re_path()
. The old location remains for backwards compatibility, without an imminent deprecation. The olddjango.conf.urls.include()
function is now importablefrom django.urls
so you can usefrom django.urls import include, path, re_path
in your URLconfs.
So, you could use the same url()
function in django 2.x
(till the complete deprication) from the older location, as
urlpatterns = [
(r'^post/(?<pk>\d+)$', views.PostDetailView.as_view(), name='post_detail'),
('account/login/', views.login, name='login')
]
Django 2.0 release notes covers your case (not exactly, but anyway) - your urls could be rewritten as follows (assuming you meant (?P<pk>\\d+)
- note the P
just after ?
):
path('post/<int:pk>/', views.PostDetailView.as_view(), name='post_detail'),
path('account/login/', views.login, name='login')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.