[Answered ]-Django redirect appends the url to the previous one

2👍

You have to perform redirect in dispatch method:

def dispatch(self, request, *args, **kwargs):
    municipio_gc = kwargs.get('geocodigo', 0)
    if int(municipio_gc) == 3304557: # Rio de Janeiro
        args = tuple([])
        return redirect('/alerta/rio/')

    return super(AlertaPageViewMunicipio, self).dispatch(request, *args, **kwargs)

Note: Your '/alerta/rio/' view doesn’t accept any parameters so avoid passing *args and **kwargs while performing redirect.

Update:

As @Alasdair pointed it’s better to get geocodigo from **kwargs instead of calling get_context_data to access value of context['geocodigo'].

0👍

You should be cautious of returning a redirect in get_context_data(), which expects to return a dict(). Try moving that logic to the get() function.

Leave a comment