Search code examples
djangodjango-rest-frameworkopenapidrf-yasg

Django route giving 404


I've got a route registered in the urls.py file of my django main app as:

router.register(r"visual/(?P<random_url>[\w]+)/$", views.LinkTest, basename="test")

and the url patterns defineded as:

urlpatterns = [
    # Admin
    path("admin/", admin.site.urls),
    # Model API Routes
    path("rest/api/latest/", include(router.urls))
]

which means I should be able to hit the viewset via the following call

http://localhost:8000/rest/api/latest/visual/random_string/

but I'm getting a 404

Can anyone tell me what I'm doing wrong?


Solution

  • The catch here is although your url visual/random_string/ is matching with regex visual/(?P<random_url>[\w]+)/$, router adds /$ automatically to your specified regex.

    Hence, internally, your regex is getting converted to visual/(?P<random_url>[\w]+)/$/$ which is not matching with visual/random_string/.

    So, remove /$ from your regex and just keep the follwing code.

    router.register(r"visual/(?P<random_url>[\w]+)", views.LinkTest, basename="test")