[Answered ]-Django view chaining/redirecting best practices

2👍

You could simply use a conditional Django redirect from the shortcuts library? There are docs here for that.

Something like:

from django.shortcuts import redirect

def my_view(request):
    if something:
        return redirect('something-view')
    elif something_else:
        return redirect('something-else-view')
    else:
        # do something else

Of course you’d have to adapt this to the kind of view you are using (Class Based / Function Based).

You could also perform the switch with a query parameter if you want to use the same urls and views:

def my_view(request):
    if something:
        return redirect('something-view', type='thing')
    elif something_else:
        return redirect('something-view', type='else')
    else:
        # do something else

Then test for type in your something-view. It’s not very secure of course (the user could change the type parameter), so you’d want to check what type of user they were again within the something-view to make sure they weren’t trying to see the version they shouldn’t.

And finally, option three is to redirect them both to the same dashboard, and they rely solely on a check of the user’s type within the dashboard view to determine what content is shown to them. This is the most secure option, as the logic is kept on the server and out of the users’ control :). This is also a very common idiom for views, for example, many views behave differently if there is a current user or not.

Leave a comment