Django 2.0 url() to path()

余生长醉 提交于 2019-12-23 10:33:07

问题


I am currently learning Django. Until now I was working with Django 1.1 but now I am working with Django 2.0. Django 2.0 uses path() instead of url() and I don't quiet understand that.

In Django 1.1 my urls looked like this:

url(r'^about/$', views.AboutView.as_view(), name='about'),

Now with Django 2 it looks like this

path('about/', views.AboutView.as_view(), name='about'),

So far so good but I just don't undertand how I can convert this

url(r'^post/(?P<pk>\d+)$', views.PostDetailView.as_view(), 
name='post_detail'),

So that it works with the new version. Just chagning url to path doesn't work, and changing url to re_path doesn't work either. Can someone help me with that Problem?

Thanks in advance


回答1:


The regular expressions are to be put in different way.

path('post/<int:pk>', views.PostDetailView.as_view(), name='post_detail'),

I just tried and tested this with the same url that you have, in one of my projects and it works. They have made the url more simpler and readable by letting to use the keyword int there.

This is the new method to do it, Please read the release notes they have clearly mentioned these changes.



来源:https://stackoverflow.com/questions/48123510/django-2-0-url-to-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!