Django URL Pattern Syntax - raw string

361 views Asked by At

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?

1

There are 1 answers

0
fanni On

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.