I am trying to learn Django. However, that tutorial is using url() function inside url.py rather than path. I was checking documentation about path() but I am a bit confused.

Like, what is the equivalent of raw string search for

url(r'^admin/$', 'views.about') 

in path

like how ^ and $ wild card characters are mapped in the path() function.

1

1 Answer

django.urls.path() function is a simpler, more readable syntax.

Lets take an example of how we write url():

url(r'^bio/(?P<username>\w+)/$', views.bio) 

Now the same url logic can be written using path() as

path('bio/<username>/', views.bio, name='bio'), 

So you can see that path is much simpler to understand since there is no regex involved.

To write regex you need to use re_path

re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio') 

From the documentation about url()

This function is an alias to django.urls.re_path(). It’s likely to be deprecated in a future release.

Hence you try to use path() and re_path() instead of url()

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy