[Fixed]-When should I use 'path' over 're_path'?

28👍

re_path is an implementation of the ‘old’ way of handling urls, which was previously (version <2) done by url from django.conf.urls.

See the paragraph in Django 2.0 release notes about this.

This being said, I recommend using path whenever you can!

The reasons I see:

  1. path was introduced with the goal of making things simpler, which is clearly the direction the Django devs want to go. So, when using path you are following this direction, thus minimizing the risk of having to adapt your codebase to new changes.

  2. While path is not just a result of an attempt of making things simpler, it actually really does make things simpler and more readable, which is a good reason alone to why path should be preferred if both choices are an option.

Now re_path exists for reasons, so there are cases when using re_path might still be a better option. One scenario is clearly when require very customized converter and reach the limit of what is feasible with custom converters for ‘path’. Yet another scenario to use re_path could be when upgrading a system with rather complex url-converters form a Django 1.x to a 2.x: Simply replacing the url with re_path commands can be much more time-efficient and thus be a desirable approach.

👤j-i-l

2👍

You got it. The newer path syntax makes for much cleaner URL patterns. You can write your own path converters, too, so that more of your paths can use path instead of re_path.

👤Franey

Leave a comment