I'm using a Jinja2 Template with FastAPI. All I want to know is how to implement a redirect action in Jinja2 template without using JavaScrpit?
If the variable I set exists, I would like to force a page redirection.
{% if my_var %}
// What value should I enter here?
{% endif %}
Case in my titles with java :
<c:if test="${!emtpy(my_var)}">
<% response.sendRedirect("/new/url"); %>
</c:if>
Case in my PHP :
if(!empty($my_var) ){
header('Location:/new/url');
exit;
}
If there is no way, I have to use JavaScript, but I don't want to use this method at all, as some of people deactivate JavaScript in their browsers.
CodePudding user response:
Since you wouldn't like to use JavaScript, you may use a <meta> HTML element/tag with the http-equiv attribute set to refresh. This instruction specifies:
The number of seconds until the page should redirect to another - only if the
contentattribute contains a non-negative integer followed by the string';url=', and a valid URL.
Hence, the value in the content is the delay before the browser redirects the user to the new page. To redirect immediately, set this parameter to 0 seconds, as shown in the example below.
The <meta> tag should be placed inside a <head> element; however, in HTML 5 it is permitted to use <meta> tags inside a <body> tag as well, as long as there is an itemprop attribute present.
Example
<head>
{% if my_var %}
<meta http-equiv="refresh" content="0; url='https://stackoverflow.com'" />
{% endif %}
</head>
