2

What is the difference between:

path('index/', views...

And

path(r'^index/$', views...

I use the first example but I see everyone using the raw string syntax in examples I look up. Are there any differences between the way Django handles the two examples?

  • The second one not path but re_path which is regex pattern check out https://docs.djangoproject.com/en/4.0/topics/http/urls/#using-regular-expressions – Pavan Kumar T S Jun 12 '22 at 10:13

1 Answers1

1

The path method is the full match method, so when your path will be exactly as described, you will have the possibility to handle the request with the view that you've described.

The re_path allows you more control with regular expressions. Let's extend your example r"^index/?$", here "?" sign allows the user to use the trailing slash "/" at the end of the URL or not use.

You can play with regexp here: https://regex101.com

The Django documentation about re_path is here: https://docs.djangoproject.com/en/dev/topics/http/urls/#using-regular-expressions

The business needs could be very complex and maybe you won't be able to make a match for a needed URL format with just the path features, but in 99% this option will be enough.

fanni
  • 1,149
  • 8
  • 11