Returns a response object (a WSGI application) that, if called, redirects the client to the target location. Supported codes are 301, 302, 303, 305, 307, and 308. 300 is not supported because it's not a real redirect and 304 because it's the answer for a request with a request with d
(
location: str, code: int = 302, Response: type[Response] | None = None
)
| 233 | |
| 234 | |
| 235 | def redirect( |
| 236 | location: str, code: int = 302, Response: type[Response] | None = None |
| 237 | ) -> Response: |
| 238 | """Returns a response object (a WSGI application) that, if called, |
| 239 | redirects the client to the target location. Supported codes are |
| 240 | 301, 302, 303, 305, 307, and 308. 300 is not supported because |
| 241 | it's not a real redirect and 304 because it's the answer for a |
| 242 | request with a request with defined If-Modified-Since headers. |
| 243 | |
| 244 | .. versionadded:: 0.6 |
| 245 | The location can now be a unicode string that is encoded using |
| 246 | the :func:`iri_to_uri` function. |
| 247 | |
| 248 | .. versionadded:: 0.10 |
| 249 | The class used for the Response object can now be passed in. |
| 250 | |
| 251 | :param location: the location the response should redirect to. |
| 252 | :param code: the redirect status code. defaults to 302. |
| 253 | :param class Response: a Response class to use when instantiating a |
| 254 | response. The default is :class:`werkzeug.wrappers.Response` if |
| 255 | unspecified. |
| 256 | """ |
| 257 | if Response is None: |
| 258 | from .wrappers import Response |
| 259 | |
| 260 | html_location = escape(location) |
| 261 | response = Response( # type: ignore[misc] |
| 262 | "<!doctype html>\n" |
| 263 | "<html lang=en>\n" |
| 264 | "<title>Redirecting...</title>\n" |
| 265 | "<h1>Redirecting...</h1>\n" |
| 266 | "<p>You should be redirected automatically to the target URL: " |
| 267 | f'<a href="{html_location}">{html_location}</a>. If not, click the link.\n', |
| 268 | code, |
| 269 | mimetype="text/html", |
| 270 | ) |
| 271 | response.headers["Location"] = location |
| 272 | return response |
| 273 | |
| 274 | |
| 275 | def append_slash_redirect(environ: WSGIEnvironment, code: int = 308) -> Response: |