6

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

mait.taim
  • 134
  • 1
  • 12
  • 2
    First, `url()` still exists, and second `re_path()` is *exactly* the same as `url()`; they are aliases to the same function. So if your code works now, there is no way it "doesn't work" with `re_path()`. – Daniel Roseman Jan 06 '18 at 12:46

1 Answers1

12

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.

Satendra
  • 6,755
  • 4
  • 26
  • 46
Lucid Polygon
  • 542
  • 9
  • 26