[英]Url mapping in django 2.0
so moving from Django 1.9 to Django 2 is not so smooth for me. 所以从Django 1.9迁移到Django 2对我来说并不顺利。 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? Django 2.0使用
path
而不是URL,如何将这些URL模式转换为与Django 2.0兼容?
url(r'^post/(?<pk>\\d+)$',)views.PostDetailView.as_view(), name ='post_detail'),
url('account/login/', views.login, name ='login')
Thanks 谢谢
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 您可以使用行为与
url()
相同的re_path()
from django.urls import re_path
urlpatterns = [
re_path(r'^post/(?<pk>\d+)$', views.PostDetailView.as_view(), name='post_detail'),
re_path(r'account/login/', views.login, name='login')
]
From the doc, ( What's new in Django 2.0 ) 从文档中,( Django 2.0的新功能 )
The
django.conf.urls.url()
function from previous versions is now available asdjango.urls.re_path()
.以前版本中的
django.conf.urls.url()
函数现在可以作为django.urls.re_path()
。 The old location remains for backwards compatibility, without an imminent deprecation.保留旧位置是为了向后兼容,而不即将淘汰。 The old
django.conf.urls.include()
function is now importablefrom django.urls
so you can usefrom django.urls import include, path, re_path
in your URLconfs.现在可以
from django.urls import include, path, re_path
旧的django.conf.urls.include()
函数from django.urls
因此您可以from django.urls import include, path, re_path
URLconfs中的from django.urls import include, path, re_path
。
So, you could use the same url()
function in django 2.x
(till the complete deprication) from the older location, as 因此,您可以在旧版本的
django 2.x
使用相同的url()
函数(直到完全剥夺为止),如下所示:
from django.conf.urls import url
urlpatterns = [
url(r'^post/(?<pk>\d+)$', views.PostDetailView.as_view(), name='post_detail'),
url('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 ?
): Django 2.0发行说明涵盖了您的情况(不完全是,但无论如何)-您的网址可以按以下方式重写(假设您的意思是
(?P<pk>\\d+)
-注意紧跟在?
之后的P
):
path('post/<int:pk>/', views.PostDetailView.as_view(), name='post_detail'),
path('account/login/', views.login, name='login')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.