Monday, October 29, 2012

Re: Reverse and kwargs...

On Mon, Oct 29, 2012 at 8:11 PM, Lachlan Musicman <datakid@gmail.com> 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,}))

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




--
@andredieb

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