python redirect to another view
django, python, redirect
Solution
You need to use reverse to build up your URL to redirect to:
from django.urls 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)
This assumes you have a named url pattern that accepts a keyword argument of 'devise', 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'),
)
That named parameter will look for one or more words and hypens, like a slug. You might want to change that to suit your needs.
Problem
I want to redirect from a view to another view and passing 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'm new to django development, so i will appreciate your help.