Redirect and Render in Django

0

I have a views.py file that has the following method:

def alterar(request, pk):
    cliente = get_object_or_404(Cliente, pk=pk)    
    if request.method == 'POST':
        form = ClienteForm(request.POST, instance = cliente)
            if form.is_valid():
                cliente = form.save(commit=False)
                cliente.save()
                cliente_listar = Cliente.objects.all()
                return render(request, 'cliente/listar.html', {'cliente' : cliente_listar}) 
    else:
        form = ClienteForm(instance=cliente)
        return render(request, 'cliente/cadastro.html', {'form' : form})

My problem is in render() of POST , because returning url from list still has the following url : http://127.0.0.1:8000/cliente/alterar/5/ , but with the list page , the right one for me would have to be: http://127.0.0.1:8000/cliente/listar/ .

In some searches I found redirect() but equal to url is in this format: http://127.0.0.1:8000/cliente/alterar/5/cliente/listar.html where it ends up giving error.

I'm using Python 3.5 and Django 1.10

    
asked by anonymous 02.11.2016 / 16:19

1 answer

1

The render () function does not change the url, only what is displayed by it. The correct one would really be you to use redirect () in conjunction with reverse (). The work of the reverse is to find the complete url by name and not by the link, as well as to mount it with the parameters. Here's an example:

return HttpResponseRedirect(reverse('listar', kwargs={'cliente': 5}))

Of course it will depend on how your urls.py is and the parameters that your url "list" needs.

Source: link

    
02.11.2016 / 16:27