I want people to be redirected to the route /profil when they join the app if they are not registered. The problem is that either CSS and Javascript won't load on the page (whereas they do when I delete the following code) :
@app.before_request
def before_request_func():
global ip_addr
ip_addr=request.environ.get('HTTP_X_FORWARDED_FOR', request.remote_addr)
if not(ip_addr in [i[2] for i in auth.getUsers()]) and request.path!='/profil':
return redirect(url_for('profil'))
More, the server shell prints :
127.0.0.1 - - [16/Mar/2022 20:28:23] "GET /profil HTTP/1.1" 200 -
127.0.0.1 - - [16/Mar/2022 20:28:23] "GET /static/css/style.css HTTP/1.1" 302 -
127.0.0.1 - - [16/Mar/2022 20:28:23] "GET /static/javascript/script.js HTTP/1.1" 302 -
127.0.0.1 - - [16/Mar/2022 20:28:23] "GET /profil HTTP/1.1" 200 -
127.0.0.1 - - [16/Mar/2022 20:28:23] "GET /profil HTTP/1.1" 200 -
Here are my other source codes :
base.html
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport" >
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='css/style.css') }}">
<link rel="icon" type="image/png" href="../static/images/logo.png" />
<title>{% block title %} {% endblock %}</title>
</head>
<body>
<canvas id="c" class="bg"></canvas>
<div id="profil">{{username}}</div>
<div id="content">
{% block content %} {% endblock %}
<a href="{{url_for('index')}}">
</div>
<div class="prospectus">
<div class="trademark">
<p class="trademark_style">DECRYPTO </p>
<p>©2022 Décrypto</p>
</div>
</div>
</a>
<script src="../static/javascript/script.js"> </script>
</body>
</html>
profil.html
{% extends 'base.html' %}
{% block title %}Authentification{% endblock %}
{% block content %}
<h1>Hello World</h1>
{%endblock%}
If someone has any idea on how to fix it... Thank you
After some more reflexions, I figured out the problem which is quite simple yo solve : when the client was requesting CSS, it was redirected to the profile route (making an infinite loop). To fix it, we can simply modify the condition as following :
@app.before_request
def before_request_func():
global ip_addr
ip_addr=request.environ.get('HTTP_X_FORWARDED_FOR', request.remote_addr)
if not(ip_addr in [i[2] for i in auth.getUsers()]) and not(request.path in ['/profil','/static/CSS/style.css','/static/javascript/script.js']):
return redirect(url_for('profil'))