Nuevakenia
Nuevakenia

Reputation: 75

Return redirect wont pass parameters

i cant find solution to this simple problem. urls.py

path('board/<id>/', board, name="board"),

views.py

def index(request):
    if request.method == 'GET':
        usuario = request.user
        last_board = Usuario.objects.filter(user=usuario.id).values_list("last_board", flat=True)
        if usuario.is_authenticated:
            return redirect('/board/', last_board )
    return render(request, "index.html")

i tried with get, post, without a request.method but its just simple dont pass the arguments, i literally have the same line on another method and works perfectly.

Error

url returned: 127.0.0.1/8000/board/
TypeError at /board/
tablero() missing 1 required positional argument: 'id'

Upvotes: 1

Views: 345

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476503

With redirect, you can use the name of the path, and then pass positional and/or keyword parameter.

Another problem is that you use .filter(…) [Django-doc], this thus means that you retrieve a collection of objects (that can contain zero, one, or more last_boards. YOu should retrieve only one, so:

def index(request):
    usuario = request.user
    if request.method == 'GET' and usuario.is_authenticated:
        usr = get_object_or_404(Usuario, user=usuario.id)
        #  name of the view ↓
        return redirect('board', id=usr.last_board)
    return render(request, "index.html")

Upvotes: 1

Related Questions