Skip to main content

redirect

Return an HttpResponseRedirect to the appropriate URL for the arguments passed.

The arguments could be:

* A model: the model's `get_absolute_url()` function will be called.

* A view name, possibly with arguments: `urls.reverse()` will be used
to reverse-resolve the name.

* A URL, which will be used as-is for the redirect location.

Issues a temporary redirect by default. Set permanent=True to issue a
permanent redirect. Set preserve_request=True to instruct the user agent
to preserve the original HTTP method and body when following the redirect.
def redirect(
to: Union[Model, str, Callable],
*args: Any,
permanent: bool = False,
preserve_request: bool = False,
max_length: int = MAX_URL_REDIRECT_LENGTH,
**kwargs: Any
) - > HttpResponseRedirect

Return an HttpResponseRedirect to the appropriate URL for the arguments passed.

Parameters

NameTypeDescription
toUnion[Model, str, Callable]A model instance with get_absolute_url(), a view name, or a specific URL string to which the user should be redirected.
*argsAnyPositional arguments passed to the URL reverse resolution function.
permanentbool = FalseIf True, returns an HttpResponsePermanentRedirect (HTTP 301) instead of a temporary redirect (HTTP 302).
preserve_requestbool = FalseIf True, instructs the user agent to maintain the original HTTP method and body when following the redirect (HTTP 307 or 308).
max_lengthint = MAX_URL_REDIRECT_LENGTHThe maximum allowed length for the redirect URL to prevent URI-based attacks.
**kwargsAnyKeyword arguments passed to the URL reverse resolution function.

Returns

TypeDescription
HttpResponseRedirectAn instance of HttpResponseRedirect or HttpResponsePermanentRedirect pointing to the resolved URL.