Monday, October 29, 2012

Re: Reverse and kwargs...

On Tue, Oct 30, 2012 at 11:14 AM, Nikolas Stevenson-Molnar
<nik.molnar@consbio.org> wrote:
> You're close. Your reverse calls should be:
>
> return redirect(reverse('student-reports', kwargs={'year': year}))

Thanks - that makes sense. I might have even done that previously, but
I am still getting errors - I must have mistakenly attributed the
error.

New error is:
Exception Type: NoReverseMatch
Exception Value: Reverse for 'student-reports' with arguments '()' and
keyword arguments '{'year': 2013}' not found.

but my urls.py includes these lines:
url(r'^students/reports/$', student_reports, name='student_reports'),
url(r'^students/reports/(?P<year>\d{4})/$', student_reports,
name='student_reports'),


and the view:
def student_reports(request, year=None):
year = year or datetime.date.today().year
...

What am I doing wrong now?

Cheers
L.

>
>
> _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.
>



--
...we look at the present day through a rear-view mirror. This is
something Marshall McLuhan said back in the Sixties, when the world
was in the grip of authentic-seeming future narratives. He said, "We
look at the present through a rear-view mirror. We march backwards
into the future."

http://www.warrenellis.com/?p=14314

--
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