Django based url routing

Hello!

I would really like a django app I'm developing to route a url to a logic based response outside of the url bar.

I would like to be able to take into account the state of the application (logged in user and his session, database content). This would mean that two users can visit the exact same URL, but depending on the type of account they have (for example), they will receive different responses.

Example

Say my application has classes User

and Admin

. Both of them will frequent http://some-domain.com/dashboard

, but they see completely different things there. In addition, under the dashboard

will display the sub-URL, for example /dashboard/comments

, /dashboard/friends

. Again, these will be completely different views depending on the user class.

I am currently doing something like this:

(urls.py)

urlpatterns = [
    url(r'^dashboard/', include([
        url(r'^$', render_dashboard),
        url(r'^settings/$', render_dashboard_settings),
        url(r'^friends/$', render_dashboard_friends),
    ])),
]

      

The problem with this setting is that there is no way to account for the current user. It can still be made to work because I can route all kinds of user accounts to the same template and use there {% if ... %}

to serve different content to different users. I could also make the name of the template to be rendered dynamic in the call django.shortcuts.render

. But I don't want either one or the other. I want to be able to differentiate between users at an earlier level of url mapping.

My question

What I want to do is extend the functionality of the method url

to take into account, and a route based on that functionality. Ideally, something like this:

urlpatterns = [
    user_class_based_url(r'', {

        'Admin': include([
            url(r'^dashboard/', include([
                url(r'^$', render_admin_dashboard),
                url(r'^settings/$', render_admin_dashboard_settings),
                url(r'^friends/$', render_admin_dashboard_friends),
            ])),
        ]),

        'User': include([
            url(r'^dashboard/', include([
                url(r'^$', render_user_dashboard),
                url(r'^settings/$', render_user_dashboard_settings),
                url(r'^friends/$', render_user_dashboard_friends),
            ])),
        ]),

        'SomeOtherUserClass': include([.....]),
    }),
]

      

Now different render functions are called for the same url, but different user classes. Unfortunately, user_class_based_url

this is what I have completely stated. Ideally there user_class_based_url

would be a custom function that I could write. Thus, I could write other similar functions that take into account the state of the application in other ways.

QUESTION: Is there a way to get this functionality?

In response to "you don't even have to design it"

The example I gave is smart enough to get my question faster. I need the functionality I described about because it will be incredibly useful for the application I'm building, and I'm sure it would be a better approach than the alternative.

Note

I am already using the django rest framework, in case it can be used to implement this design.

+3


source to share


2 answers


There is no way in Django to do this. There was this discussion recently about creating a new API for the url manager, but any changes would be in a future version of Django.



0


source


If you need different templates for the same url template, do not reinstall templates in urls.py

. Instead, go to a view function (or alternatively a class-based view ) in views.py

which submits different templates based on your criteria. This is exactly what it is intended for views.py

and how it is commonly used.

For redirecting a URL to a view function, it's best to consult part 3 of the Django course . (If you're learning Django, you might want to start in the first part.)



Adapting the example , when a shortcut is introduced in the tutorial render

, a view that sends different templates might look something like this:

from django.shortcuts import render

from .models import Question


def index(request):
    if request.user.is_admin():
        return render(request, 'polls/admin-index.html', {})
    else:
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        context = {'latest_question_list': latest_question_list}
        return render(request, 'polls/index.html', context)

      

+1


source







All Articles