Nested urls inside the same app?


I wanted to be able to reverse nested urls inside the same app like:

reverse("products:regular:create") == /products/regular/new/

I found include can be passed an app_namespace, so I made that. (Reference)


app_name = "products"

regulars_urlpatterns = [
    # /products/regular/create/
    path("new/", RegularProductCreateView.as_view(), name="create"),

urlpatterns = [
    # /products/...
    path("/", ProductListView.as_view(), name="list"),
    # /products/regular/...
    path("regular/", include((regulars_urlpatterns, "regular"))),


Is this right? Will there be some issue with this “hack”?
I feel like I’m tricking Django into believing those urls belongs to a “regular” app.


Is there a reason why you think there’s something wrong with this? (What makes you think you’re “tricking” Django in some way?)

If it works the way you want (need) it to and doesn’t throw any errors (or warnings), then I’m not sure what you would be concerned about.

While writing this question I remembered that you once answered me something like “If it works for you, what is the problem”? :sweat_smile:

I thought that because I’m passing “regular” as an app_namespace, but “regular” is not a Django app. (Reference)

I agree with Ken. If it’s readable, works and makes your life easier it’s quality code.

Ok, that’s what I was missing / not understanding from your question (“regular” not being a “real” app). Nothing strikes me as “wrong” about that, but it’s not something I think I’ve ever done.

(In reality, I tend to use the answer “if it works for you…” to cover the cases where I don’t see anything wrong and don’t envision any hidden edge cases that might cause problems in the future.)

1 Like

I’m using it with no problems so far but, as it seems like an unexpected use, there may be things I’m not aware of.

Thanks for your comments @CodenameTim @KenWhitesell.