178

In a django online course, the instructor has us use the url() function to call views and utilize regular expressions in the urlpatterns list. I've seen other examples on youtube of this. e.g.

from django.contrib import admin
from django.urls import include
from django.conf.urls import url

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^polls/', include('polls.urls')),
]


#and in polls/urls.py

urlpatterns = [        
    url(r'^$', views.index, name="index"),
]

However, in going through the Django tutorial, they use path() instead e.g.:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name="index"),        
]

Furthermore regular expressions don't seem to work with the path() function as using a path(r'^$', views.index, name="index") won't find the mysite.com/polls/ view.

Is using path() without regex matching the proper way going forward? Is url() more powerful but more complicated so they're using path() to start us out with? Or is it a case of different tools for different jobs?

FrostedCookies
  • 2,253
  • 2
  • 13
  • 15
  • 3
    If you are following a tutorial, I would use whichever style the tutorial uses, to prevent mistakes when switching between `path()` and `url()`. If you want or need to use regexes, then you must use `re_path()` or `url()`. Beyond that, I think it's really up to you which one you choose. – Alasdair Dec 22 '17 at 21:56

6 Answers6

216

From Django documentation for url

url(regex, view, kwargs=None, name=None) This function is an alias to django.urls.re_path(). It’s likely to be deprecated in a future release.

Key difference between path and re_path is that path uses route without regex

You can use re_path for complex regex calls and use just path for simpler lookups

Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
iklinac
  • 14,944
  • 4
  • 28
  • 30
94

The new django.urls.path() function allows a simpler, more readable URL routing syntax. For example, this example from previous Django releases:

url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive)

could be written as:

path('articles/<int:year>/', views.year_archive)

The django.conf.urls.url() function from previous versions is now available as django.urls.re_path(). The old location remains for backwards compatibility, without an imminent deprecation. The old django.conf.urls.include() function is now importable from django.urls so you can use:

from django.urls import include, path, re_path

in the URLconfs. For further reading django doc

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Sylvernus Akubo
  • 1,487
  • 11
  • 13
  • 13
    A little misleading since your `path()` example allows non-four-digit numbers. – BrainCore Apr 27 '19 at 12:12
  • The `//` signifies that the year should be in integer, i suggest you look through the official documentation. – Sylvernus Akubo Apr 28 '19 at 21:40
  • 7
    @silvablaze As BrainCore stated, `int` is not equivalent to `[0-9]{4}`. Whether it is used for integers or not has no bearing on this relationship. Perhaps if you replaced `[0-9]{4}` with `[0-9]+` your comparison might be less inaccurate, although you might still need to deal with the type of the end result (str vs int?). – Jan Kyu Peblik Jul 05 '19 at 18:45
21

path is simply new in Django 2.0, which was only released a couple of weeks ago. Most tutorials won't have been updated for the new syntax.

It was certainly supposed to be a simpler way of doing things; I wouldn't say that URL is more powerful though, you should be able to express patterns in either format.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
10

Regular expressions don't seem to work with the path() function with the following arguments: path(r'^$', views.index, name="index").

It should be like this: path('', views.index, name="index").

The 1st argument must be blank to enter a regular expression.

ack
  • 1,181
  • 1
  • 17
  • 36
9

Path is a new feature of Django 2.0. Explained here : https://docs.djangoproject.com/en/2.0/releases/2.0/#whats-new-2-0

Look like more pythonic way, and enable to not use regular expression in argument you pass to view... you can ue int() function for exemple.

Denis Cottin
  • 313
  • 2
  • 11
5

From v2.0 many users are using path, but we can use either path or url. For example in django 2.1.1 mapping to functions through url can be done as follows

from django.contrib import admin
from django.urls import path

from django.contrib.auth import login
from posts.views import post_home
from django.conf.urls import url

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^posts/$', post_home, name='post_home'),

]

where posts is an application & post_home is a function in views.py

Nani Chintha
  • 491
  • 5
  • 3