danatron
danatron

Reputation: 717

reverse works, but redirect fails

I've read the docs like 5 times, I can't see whats missing here.

I have a situation where reverse() works fine, but the same params passed to redirect() fails with an error stating the address cannot be resolved.

reverse("app:submission_thanks", kwargs={ "data": survey.data.slug, "survey": survey.slug })

redirect("app:submission_thanks", kwargs={ "data": survey.data.slug, "survey": survey.slug })

My url is:

url(r'^(?P<data>[-\w]+)/(?P<survey>[-\w]+)/thanks/$',
         TemplateView.as_view(template_name="pasteur/submission_thanks.html"),
         name="submission_thanks",
    ),

Upvotes: 0

Views: 139

Answers (2)

Aamir Rind
Aamir Rind

Reputation: 39659

Pass reversed url in redirect:

return redirect(
    reverse("app:submission_thanks",
    kwargs={ "data": survey.data.slug, "survey": survey.slug }))

Upvotes: 0

bikeshedder
bikeshedder

Reputation: 7487

Redirect has a different method signature:

redirect("app:submission_thanks", data=survey.data.slug, survey=survey.slug)

See shortcut redirect examples in the Django docs.


Diving into the code a little: redirect(to, *args, **kwargs) calls resolve_url(to, *args, **kwargs) which calls urlresolvers.reverse(to, args=args, kwargs=kwargs). So this is definitely correct.

You do not need to wrap the args in a list or kwargs in a dictionary when using the resolve_url or redirect shortcut.

Upvotes: 1

Related Questions