Python redirects to another view

I want to redirect from view to another view and pass data, but no chance. Here are my codes:

def affiche(request): if request.method == 'POST': form = AfficheForm(request.POST) if form.is_valid(): Select = form.cleaned_data['Select'] if Select == '1': return redirect('affiche_all', devise='EURO') def affiche_all(request, devise): data = websvc(devise) return render_to_response('affiche_all.html', {'data': data}, RequestContext(request)) 

I am new to django development, so I will be grateful for your help.

+4
source share
2 answers

You need to use the backlink to create the url to redirect to:

 from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect def affiche(request): form = AfficheForm(request.POST or None) if request.method == 'POST': if form.is_valid(): Select = form.cleaned_data['Select'] if Select == '1': url = reverse('affiche_all', args=(), kwargs={'devise': 'EURO'}) return HttpResponseRedirect(url) 

It is assumed that you have a named url pattern that accepts the argument of the 'devise' keyword, as such:

 from django.conf.urls import url, patterns urlpatterns = patterns('your_app.views', url(r'^some-path/(?P<devise>[-\w]+)/$', 'affiche_all', name='affiche_all'), ) 

This named parameter will look for one or more words and hypens, like slug. You might want to change this to suit your needs.

+4
source

try it

 return HttpResponseRedirect(reverse('affiche_all', devise=('EURO',))) 
+2
source

Source: https://habr.com/ru/post/1468973/


All Articles