django redirect to another view with context

In django You can not pass parameters with redirect. Your only bet is to pass them as a part of URL.

def foo(request):

    context['bar'] = 'FooBar'

    redirect(reverse('app:view', kwargs={ 'bar': FooBar }))

in your html you can get them from URL.


I would use session variables in order to pass some context through a redirect. It is about the only way to do it outside of passing them as part of the url and it is the recommended django option.

def foo(request):
    request.session['bar'] = 'FooBar'
    return redirect('app:view')

#jinja
{{ request.session.bar }}

A potential pitfall was pointed out, whereas the session variable gets used incorrectly in a future request since it persists during the whole session. If this is the case you can fairly easily circumvent this problem in a future view in the situation it might be used again by adding.

if 'bar' in request.session:
    del request.session['bar']