[Fixed]-Why is Regex used in Django Routes?

1๐Ÿ‘

By routes you mean the urls contained in the urls.py files? If so, this is a very open question. Regular expressions are very useful and pretty simple to use in this context. Unless you are creating a static website you are probably going to need an almost uncountable number of url addresses for your whole site. These addresses are easily generated with the help of regular expressions.

Also, Django links each url address with a specific view, the use of regex makes it simple.

Example:

url(r'^alterar-dados/', views.update_user, name='update_user'), # In this case, everything that starts with alterar-dados/ will be handled by the view "update_user". And then, the same template can be used by all of these addresses.

OBS.: If that was not what you asked, sorry!

๐Ÿ‘คGabriel Belini

0๐Ÿ‘

In Django, you need regular expressions in urls.py file. This is how you access your html pages in the template. Django urls need to follow some order in order to work properly and that order is defined by regex. Regex has a lot of rules that form a search pattern.

๐Ÿ‘คBolshoi Booze

    Leave a comment