Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 2.0 url() to path()

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

like image 799
mait.taim Avatar asked Jan 06 '18 01:01

mait.taim


1 Answers

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.

like image 186
Lucid Polygon Avatar answered Sep 23 '22 05:09

Lucid Polygon