Reverse and kwargs...

259 views
Skip to first unread message

Lachlan Musicman

unread,
Oct 29, 2012, 7:11:58 PM10/29/12
to django...@googlegroups.com
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.

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

Nikolas Stevenson-Molnar

unread,
Oct 29, 2012, 7:14:39 PM10/29/12
to django...@googlegroups.com
You're close. Your reverse calls should be:

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


_Nik

André Dieb

unread,
Oct 29, 2012, 7:14:14 PM10/29/12
to django...@googlegroups.com
This {year=year} syntax looks odd to me. I recommend to use either dict(year=year) or {'year': year}.
 


What is the correct syntax?

Cheers
L.

--
...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...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
@andredieb

Lachlan Musicman

unread,
Oct 29, 2012, 7:38:21 PM10/29/12
to django...@googlegroups.com
On Tue, Oct 30, 2012 at 11:14 AM, Nikolas Stevenson-Molnar
<nik.m...@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.
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>



Christophe Pettus

unread,
Oct 29, 2012, 7:44:52 PM10/29/12
to django...@googlegroups.com
- vs _ ?
-- Christophe Pettus
x...@thebuild.com

Lachlan Musicman

unread,
Oct 29, 2012, 7:52:02 PM10/29/12
to django...@googlegroups.com
On Tue, Oct 30, 2012 at 12:44 PM, Christophe Pettus <x...@thebuild.com> wrote:
> - vs _ ?
>

I'm sorry, I'm not experienced enough or sufficiently knowledgeable
about python or django to understand your response.

I understand that _ has some relevance as an indicator of global vars
and functions in some way.

Could you please expand on your answer for a beginner?

Cheers
L.

Christophe Pettus

unread,
Oct 29, 2012, 8:03:45 PM10/29/12
to django...@googlegroups.com

On Oct 29, 2012, at 4:52 PM, Lachlan Musicman wrote:
> Could you please expand on your answer for a beginner?

student-reports in the reverse call vs student_reports in the url() definition?

Lachlan Musicman

unread,
Oct 29, 2012, 8:06:58 PM10/29/12
to django...@googlegroups.com
On Tue, Oct 30, 2012 at 1:03 PM, Christophe Pettus <x...@thebuild.com> wrote:
>
> On Oct 29, 2012, at 4:52 PM, Lachlan Musicman wrote:
>> Could you please expand on your answer for a beginner?

GAH! Thanks. I've been thinking that there's got to be an easier way
to get and generate these reports, so I've been thinking alot about
Meta and _ but don't fully understand it - hence my confusion.

Your solution made it work, and I'm a doofus.

cheers
L.

Christophe Pettus

unread,
Oct 29, 2012, 8:18:40 PM10/29/12
to django...@googlegroups.com

On Oct 29, 2012, at 5:06 PM, Lachlan Musicman wrote:
> Your solution made it work, and I'm a doofus.

Let he who is without having spent the entire day looking for a punctuation error in code cast the first stone. :)
Reply all
Reply to author
Forward
0 new messages