Kai
Kai

Reputation: 593

Can't redirect to another view/url

I have django app (1.8) and I want to have redirection from BookingRedirect to ArchiveListView after click link below:

<a href="{% url 'archive:list' %}" title="{% trans 'Archive' %}">
    {% trans 'Archive' %}
</a>

I recived this error:

Reverse for 'archives' with arguments '()' and keyword arguments '{'kwargs': {'year': '2014', 'month': '1'}}' not found. 1 pattern(s) tried: ['en/archive/(?P<year>[0-9]{4})/(?P<month>[0-9]+)$']

My view where I want to have redirect to another url with another view:

from django.shortcuts import redirect


class BookingRedirect(RedirectView):
    permanent = False

    def get_redirect_url(self, *args, **kwargs):
        return redirect('archive:archives', kwargs={'year': '2014', 'month': '1'})

urls:

urlpatterns = [
    url('^$', views.BookingRedirect.as_view(), name="list"),
    url(r'^/(?P<year>[0-9]{4})/(?P<month>[0-9]+)$', views.ArchiveListView.as_view(), name="archives"),
]

Upvotes: 0

Views: 101

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599600

That's not how you pass kwargs to the redirect function. You pass them directly as kwargs:

return redirect('archive:archives', year='2014', month='1')

See the docs.

Note, however, that your get_redirect_url should not be using that function at all. It needs to return a URL that the view itself then redirects to. So you need to use reverse, which does take kwargs in the format you used:

return reverse('archive:archives', kwargs={'year': '2014', 'month': '1'})

Upvotes: 1

Related Questions