Amit Pal
Amit Pal

Reputation: 11052

How to pass extra_context when redirecting in Django

My views.py:

 @login_required
 def some_views(request):
     if request.method == 'POST':
         form = AddressCreateFrom(request.POST)
         if form.is_valid():
             name = form.cleaned_data['Address']
             ip_value = form.cleaned_data['value']
             user_list = get_username(name)
             address_create = form.save()
             extra_context = {
                 'user_list': user_list
                 }
             return redirect_to(request, url=address_create.get_absolute_url())
     else:
         form = AddressCreateFrom()
     extra_context = {
         'form':AddressCreateFrom(initial={'user': request.user.pk})
         }
     return direct_to_template(request,'networks/user_form.html',extra_context)

In form.py:

 class AddressCreateFrom(forms.ModelForm):
     Address = forms.CharField(max_length=40)
     value = forms.CharField(max_length=40)
     class Meta:
         model = Network
         widgets = {
             'user': forms.HiddenInput()
           }

As you see that i am using Django model form with two extra Django form field i.e. Address and value in AddressCreateForm class. I need all of the field at the time of rendering the template.

Indeed some_views method are working fine but i also want render some extra data written in context_dictionary i.e. user_list to a requesting URL i.e. address_create.get_absolute_url().

If i am not wrong, if we are handling with the database we have to use redirect_to method. Is it possible to do that?

Upvotes: 0

Views: 3110

Answers (2)

Alasdair
Alasdair

Reputation: 308849

A redirect will return a HTTP response with status code 301 or 302, and the location to redirect to:

301 MOVED PERMANENTLY
Location: http://www.example.com/new-url/

There is no template rendered by the original view, so you can't pass extra_context to it.

The user's browser will usually follow the redirect, and request the new URL.

If you want to display information about a particular user in the next view, you have to do something like:

  1. design your URL pattern to include the user id, e.g. /users/200/,
  2. include it as a get parameter e.g. /users/?id=200, then fetch the user id from request.GET in the view.
  3. store the user_id in the session
  4. Before redirecting, create a message using the messages framework using the user data.

Then in the view that you redirect to, you can fetch the user from the database, and add it to the template context.

Upvotes: 5

Avid Coder
Avid Coder

Reputation: 18387

Context, Extra Context and POST Data will not survive the redirect.

Here is what you can do.

# before the redirect
....
request.session['user_list'] = user_list
return redirect_to(request, url=address_create.get_absolute_url())

# after the redirect (in the views.py that handles your redirect)
....
user_list = request.session['user_list']
extra_context = { 'user_list': user_list }
....
# now you have the user_list in the extra_context and can send it to the rendering engine.

Note: This solution only works for redirects within your own server.

Upvotes: 2

Related Questions