Sanket
Sanket

Reputation: 31

How to send veriables using redirect and use in other views

Im working on project and im sending data/veriable while redirecting flow from one view to another(first view/empty view)

def index(request,a=False):
    if request.method=='POST':
        return render(request,'chart-chart-js.html',{'a':True})
    return render(request,'chart-chart-js.html',{'a':a})

def login(request):
    if request.method == 'POST':
    
        form = NameForm(request.POST)
        
        if form.is_valid():
            if form.cleaned_data['username']=='Unknown' and form.cleaned_data['password']=='Unknown':
                
                return redirect(index,a=True)

Upvotes: 0

Views: 24

Answers (1)

Ajeet Ujjwal
Ajeet Ujjwal

Reputation: 133

You can not pass context variables using Redirect this way.

I can suggest two options:

  1. Send and receive as a GET request parameter

In login function:

    return redirect(reverse(index) + '?a=VALUE')

In index function:

    a = request.GET.get('a')
  1. Use Django Session

In login function:

    request.session['a'] = VALUE
    return redirect(index)

In index function:

    a = request.session.get('a')

Upvotes: 1

Related Questions