user2492364
user2492364

Reputation: 6693

How to redirect with variables in django?

How to redirect with variables in django? Please guide me, thank you.

urls.py:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^computer/$', views.computer, name='computer'),
    url(r'^result/$', views.result, name='result'),
)

This is my original views.py :

def computer(request):
    result =  Computer.objects.order_by('?')[:1]
    return render(request, 'many/result.html',{'result':result})  

And I found I problem, render will not redirect to moneymany/result.html on the url,
so if the user refresh, it will get another result on the same page.

So I have to use redirect to many/result.html .
What's the usually way to redirect in django and I have to pass variable result? I try this,but not work :

def result(request):
    return render(request, 'many/result.html')

def computer(request):
    result =  Computer.objects.order_by('?')[:1]     
    url = reverse(('many:result'), kwargs={ 'result': result })
    return HttpResponseRedirect(url)

Upvotes: 4

Views: 23405

Answers (4)

To redirect from a view to another view with data, you can use session with request.session['key'] and in this case, you don't need to modify path() in "myapp/urls.py" as shown below. Then, give the conbination of the app name "myapp", colon ":" and the view name "dest_view" which is set in path() in "myapp/urls.py" as shown below:

# "myapp/views.py"

from django.shortcuts import redirect

def redirect_view(request):
    # Here
    request.session['person'] = {'name': 'John', 'age': 27}
                    # Here
    return redirect("myapp:dest_view")
# "myapp/urls.py" 

from django.urls import path
from . import views

app_name = "myapp"

urlpatterns = [                           # This is view name
    path('dest/', views.destination_view, name="dest_view")
]

Then, this is how you get the data of "post" method:

# "myapp/index.html"

{{ request.session.person.name }} {# John #}
{{ request.session.person.age }}  {# 27 #}

Upvotes: 1

Lalmi Mohammed Issam
Lalmi Mohammed Issam

Reputation: 138

this worked with i just needed to pass url parameters as arguments

return redirect('pagename' ,param1 , param2)

my url looks like :

path('page', views.somefunction, name="pagename")

Note: in my case somefunction accept only POST parameters

Upvotes: 2

doniyor
doniyor

Reputation: 37846

you need

url(r'^result/(?P<result>[^\/]*)/$', views.result, name='result'),

and

return redirect(reverse('many:result', kwargs={ 'result': result }))

or (without changing url)

return redirect('/result/?p=%s' % result )

if you want to maintain POST data while redirecting, then it means your design isnot good. quoting Lukasz:

If you faced such problem there's slight chance that you had over-complicated your design. This is a restriction of HTTP that POST data cannot go with redirects.

Upvotes: 6

parivana
parivana

Reputation: 299

How about using redirect.

from django.shortcuts import redirect

def computer(request):
    result =  Computer.objects.order_by('?')[:1]
    return redirect('view-name-you-want', { 'result'=result })

Upvotes: 4

Related Questions