django redirect() with parameters

Rather than redirecting to the destination url, simply call the destination view function directly:

def myView(request):
    if request.user.is_authenticated():
        if request.method == 'POST':
            #my code comes here
            ....
            return anotherView(request, username, range)



def anotherView(request,username,range):
    if request.user.is_authenticated():
        #my code comes here
        ....
        return HttpResponse(something)

redirect is merely a wrapper around HttpResponseRedirect that automatically calls reverse for you to create the URL to redirect to. As a result, the parameters you pass to it, aren't arbitrary, they must be same you would pass to reverse and, specifically, only those required to create the URL.

Many people seem to have troubles understanding that data can't just be arbitrarily passed to a view. HTTP is a stateless protocol: each request exists on it's own, as if user had never been to any other page of the site. The concept of a session was created to provide a sense of "state" to a cohesive unit such as a site. With sessions, data is stored in some form of persistent storage and a "key" to look up that data is given to the client (typically the user's browser). On the next page load, the client sends the key back to the server, and the server uses it to look up the data to give the appearance of state.

As a result, if you need data from one view available in another, you need to add it to the session, do your redirect, and look up the data in the session from the next view.

Tags:

Python

Django