【问题标题】:Return redirect wont pass parameters返回重定向不会传递参数
【发布时间】:2021-06-12 16:30:24
【问题描述】:

我找不到这个简单问题的解决方案。 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")

我尝试使用 get,post,没有 request.method,但它只是简单地不传递参数,我实际上在另一个方法上有相同的行并且完美地工作。

错误

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

【问题讨论】:

    标签: python-3.x django django-views


    【解决方案1】:

    使用redirect,您可以使用路径的名称,然后传递位置和/或关键字参数。

    另一个问题是您使用.filter(…) [Django-doc],这意味着您检索对象的集合(可以包含零个、一个或多个last_boards。你应该只检索一个,所以:

    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 &downarrow;
            return redirect('board', id=usr.last_board)
        return render(request, "index.html")

    【讨论】:

    • 非常感谢,完美运行,现在我更了解重定向的工作原理了!
    猜你喜欢
    • 2020-06-04
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    相关资源
    最近更新 更多