Question

I have a some problem with routing in my django app.

The problem: There is some dynamic website and site administrator can create pages with random urls. For example, he can create a news page with url "company/news" or "store/news". Or he can create the page with feedback form with url "feedback" or "user/feedback".

So, Django needs to catch this request and show appropriate news or feedback content for these pages. How can I route the user request to the appropriate view according to the requested page functionality?

Was it helpful?

Solution

You can create view that parses your URL and chooses strategy for different types.

# urls.py
...
url(r'^dynamic-view/(?P<dynamic_view_url>.*)/$', 'dynamic_view')


# views.py
def dynamic_view(request, dynamic_view_url):
    url_parts = [p for p in dynamic_view_url.split("/") if p]
    if "feedback" in url_parts:
        return _view_for_feedback(request, url_parts)
    elif "news" in url_parts:
        return _view_for_news(request, url_parts)
    else:
        raise Http404
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top