def METHOD_NAME(
    request: HttpRequest,
    default: str = "/",
    next: None = None,
    allowed_hosts: None = None,
    params: Optional[Dict[str, str]] = None,
    keep_session: bool = False,
) -> str:
    if next is None:
        next = request.POST.get(
            "next", request.GET.get("next", request.session.get("next"))
        )
        if not keep_session and "next" in request.session:
            del request.session["next"]
    if next is None:
        keyword = request.GET.get("pk_keyword")
        if keyword and keyword.startswith("/"):
            next = keyword
    url_allowed = url_has_allowed_host_and_scheme(url=next, allowed_hosts=allowed_hosts)
    if not url_allowed:
        next = None
    if next is None and default is not None:
        if not default.startswith("/"):
            default = reverse(default)
        next = default
        url_allowed = url_has_allowed_host_and_scheme(
            url=next, allowed_hosts=allowed_hosts
        )
    if next is None or not url_allowed:
        next = request.headers.get("referer")
        url_allowed = url_has_allowed_host_and_scheme(
            url=next, allowed_hosts=allowed_hosts
        )
    if next is None or not url_allowed:
        next = "/"
    if params is not None:
        next = update_query_params(next, params)
    return next