Monday, October 29, 2012

Re: Reverse and kwargs...

You're close. Your reverse calls should be:

return redirect(reverse('student-reports', kwargs={'year': year}))


_Nik

On 10/29/2012 4:11 PM, Lachlan Musicman wrote:
> Hi,
>
> I'm struggling to get the syntax right for a url reverse function.
>
> I have a form that asks for a Model type (ChoiceField with strings)
> and a year (CharField).
>
> The logic then if/elifs the ChoiceField and then redirects to the
> appropriate report page (for statistics on the Model type), with an
> optional year. I've got the urls and the views working, but passing
> the year arg to the view via the reverse function is not obvious to
> me, and nothing I've tried seems to work?
>
> example code:
> forms.py
> class ReportRequestForm(forms.Form):
> DATA_TYPES =
> ((1,'Students'),(2,'Enrolments'),(3,'Applicants'),(4,'Staff'),(5,'Results'))
> year = forms.CharField(max_length=4)
> data_type = forms.ChoiceField(choices=DATA_TYPES)
>
> views.py
> def ReportRequestForm(self):
> ...
> if form.is_valid():
> year = int(form.cleaned_data['year'])
> data_type = form.cleaned_data['data_type']
> if data_type == '1':
> return redirect(reverse('student-reports',{year=year,}))
> elif data_type == '2':
> return redirect(reverse('applicant-reports',{year=year,}))
> elif data_type == '3':
> return redirect(reverse('enrolment-reports',{year=year,}))
> elif data_type == '4':
> return redirect(reverse('staff-reports',{year=year,}))
>
>
> What is the correct syntax?
>
> Cheers
> L.
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment