pythondjangoroutesdjango-urlsurlconf

django urlconf direct from root to included app logic


I've been trying to solve this problem for a while, but haven't been able to find a solution that works. Maybe someone can help point out a better method!

I'm using my root urlconf to redirect two requests to an included app:

url("^about/news/", include("kdi.urls")),
url("^about/recognition/", include("kdi.urls")),

And here is the kdi app's urlconf:

url(r"^$", "kdi.views.news", name="news"),

# this is the pattern that needs to change:
url(r"^$", "kdi.views.recog", name="recog"),

It seemed smarter to use the more granular redirection from root ^about/news/ and ^about/recognition to the ^$ in the app's urlconf. This worked fine with only one pattern, but I'd like to scale it to work for both.

Would something like directing ^about/ from root to the app where I could check for ^/news$ or ^/recognition$ in the kdi app be smarter? Would that also be able to use the root's catch-all ^ if no matches were made? Is it possible to check the request.path from the urlconf and then use an if statement to direct to the correct view? Or maybe use a name field in the root and then access via that name in the app's url patterns?

Just having a little trouble working out the logic with this!

edit:

removed namespace field from root urlconf to limit confusion


Solution

  • You're doing something very bizarre here and I can't work out why.

    You should only include your app URLs once. But every URL in that included file needs to be different - otherwise Django can't possibly know how to route the request.

    So, the main urls.py should be just:

    url("^about/", include("kdi.urls")),
    

    and the app ones should be:

    url(r"^news/$", "kdi.views.news", name="news"),
    url(r"^recognition/$", "kdi.views.recog", name="recog")