Laxmikant
Laxmikant

Reputation: 2216

Django - dictionary passed in redirect is not showing in template

I'm trying to redirect my signup form to the same form after saving the values also I wanted to return a dictionary in the redirect response. But the following code is not working.

urls.py

url(r'^signupform/$', views.render_signup, name='render_signup'),
url(r'^signup/$', views.sign_up_account_admin, name='sign_up_account_admin'),

views.py

def render_signup(request):
    """
    """
    data_dict = {"some_keys":"some_values"}
    return render(request, "admin.html", data_dict)



def sign_up_account_admin(request):
    """
    """
    user = None
    user, msg = save_form_data(request)
    req_dict = {"msg": msg}
    if user:
        ret = redirect('homepage') #-- This works pretty fine
    else:
        ret = redirect('/signupform/', resp = req_dict) #-- This redirects but I don't get the `resp` Objects in the template
    return ret

Html Temlpate

    <div class="container">
      =={{resp}}
      ===>>>>>>>{{resp.msg}}
    </div>

And the Output HTML

  <div class="container">
      ==
      ===>>>>>>>
   </div>           

Upvotes: 1

Views: 4694

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 47846

You can't pass random kwargs to the redirect function.

The arguments passed to redirect are the same positional or keyword arguments which you would have passed had you used reverse() to generate the url. These arguments are used to reverse resolve the url.

As mentioned in the Django docs about using redirect() function.

By passing the name of a view and optionally some positional or keyword arguments; the URL will be reverse resolved using the reverse() method.

Now, as you are passing a hardcoded url, then the argument passed along with it will get discarded.

The recommended way to use redirect() with a hardcoded url as mentioned in Django docs is:

def my_view(request):
    ...
    return redirect('/some/url/')

So, what you can do now is use to Django sessions or maybe pass a parameter in the url to access the value of resp in the render_signup view.

Solution-1 Using Django Sessions

One way is to use Django sessions to get the msg.

def sign_up_account_admin(request):
    """
    """
    user = None
    user, msg = save_form_data(request)
    req_dict = {"msg": msg}
    if user:
        ret = redirect('homepage') #-- This works pretty fine
    else:
        request.session['resp'] = msg # set in session
        ret = redirect('/signupform/')

    return ret

Then in your render_signup view, you can do:

def render_signup(request):
    """
    """
    resp = request.session.get('resp') # get the value from session
    data_dict = {'resp': resp, ..}
    return render(request, "admin.html", data_dict)

Solution-2 Pass it as url parameters

Another solution is to pass it in url as query parameters.

ret = redirect('/signupform/?resp=%s'%msg) # pass as query parameter

Then in the render_signup view, get this value from request.GET.

resp = request.GET.get('resp') # get the value of 'resp' from request.GET
data_dict = {'resp':resp, ..}

EDIT:

If you have multiple parameters to send which are in a dictionary, then you can use urllib.urlencode() to encode the dictionary and send it with the url.

encoded_string = urllib.urlencode(my_dict)
redirect('/signupform/?%s'%encoded_string) # attach the encoded string in the url

Then in your view, you can retrieve the keys of the dictionary from request.GET.

value1 = request.GET.get('key1')
value2 = request.GET.get('key2')

Upvotes: 5

Related Questions