[Django]-Precedence of Django url

5👍

Short answer: make non-overlapping patterns.

The problem is that your patterns overlap. Indeed, if the url is /api/user/signin/, and the /api/user/<str:user_uniq_id>/ is the first pattern in your list of URLs, it will match that pattern. Indeed, since the <str:user_uniq_id> can simply be unified with signin. The fact that there is another url later in the list that matches as well, is not relevant.

Therefore you should design non-overlapping patterns. So that means that no url that can be generated by one pattern, can be generated by another pattern.

For example you can design your urls as:

/api/user/details/<str:user_uniq_id>/
/api/user/signin/
/api/user/registration/
/api/user/invitation/edit/
/api/user/invitation/details/<str:uniq_id>/

By thus using /details, there is no way that a URL /api/user/sigin can match the first pattern, since it needs to contain /api/user/details as prefix.

Altnatively, you can put the /api/user/signin path first in the list of paths. But that does not look like a good idea to me. If later a user has as uniq_id simply 'signin' (yes that may look rare, but eventually it can happen), then that user can not see his/her “details” page.

2👍

You can simply place the all the predefined urls first as mentioned below,that way if the url matches with them then it will hit it,otherwise it will move to the other urls.

/api/user/sigin/

/api/user/registration/

/api/user/<str:user_uniq_id>/

/api/user/invitation/edit/

/api/user/invitation/<str:uniq_id>/


Leave a comment