[Answered ]-1 always not match in Django re_path regular expression

1👍

The update on your question explains that this is likely some caching mechanism in the browser.

I would however advise to split this up into two paths: one with an <int:my_int> parameter, and one without such parameter. These can both call the same view:

from django.urls import path

urlpatterns = [
    path('my_url/', views.my_function, name='my_function'),
    path('my_url/<int:my_id>/', views.my_function,  name='my_function')
]

In the view, you then make my_id an optional parameter that resolves to None in case it is not called with this parameter:

def my_function(request, my_id=None):
    if my_id is None:
        # do something
    else:
        # do something else

Leave a comment