A student here, currently learning Flask and learning how to use request.endpoint.
When I attempt to print it, it shows 'static' instead. I'd like it to print the current route, for example: 'index' or 'login'
Here's an example from my console:
127.0.0.1 - - [28/Apr/2024 11:12:35] "GET /static/css/style.css HTTP/1.1" 304 -
before_request
static
It seems that it's not capturing the correct route; instead, it's picking up routes for my JavaScript or CSS files and that applies to every route. I'd like it to print the current route, for example: 'index' or 'login' instead of 'static'
How can I correctly print request.endpoint, but without excluding my CSS and JS routes?"
Parts of my code:
main.py
@app.before_request
def before_request():
print("before_request")
if 'username' not in session:
print(request.endpoint)
@app.route('/', methods = ['GET', 'POST'])
def index():
if 'username' in session:
username = session['username']
print(username)
title = "Curso Flask"
return render_template('index.html', title = title,)
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>{%block title%} {%endblock%}</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<h1>Hola mundo desde mi base.html</h1>
<!--Ejemplo de flash message para recibir el response de un mensaje-->
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{message}}</li>
{%endfor%}
</ul>
{%endif%}
{%endwith%}
{% block content %}
{% endblock %}
<script type="text/javascript" src="{{ url_for('static', filename='js/jquery.js') }}"></script>
</body>
</html>
Index.html
{% extends 'base/base.html'%}
{% block title %} {{ title }} {%endblock%}
{% block content %}
<h2>Hola mundo desde mi index.html</h2>
<!--Esto es para importar el macro-->
{% from "_macro.html" import show_list_h %}
<!--Esto es para llamar al macro-->
{{ show_list_h('Hola mundo desde mi macro')}}
{% endblock %}
There's more, but I condensed it to what I think is most relevant.
To me it seems its working correctly.
When a browser makes a request, it will request the different resources you have linked in your HTML. So if in your index.html
you have .js files, .css and a favicon then the browser will make multiple request to get the resources to display correctly your website.
For example if my index.html
had a .css and a favicon the output looks like this:
index
127.0.0.1 - - [28/Apr/2024 22:56:34] "GET / HTTP/1.1" 200 -
static
127.0.0.1 - - [28/Apr/2024 22:56:34] "GET /static/style.min.css HTTP/1.1" 200 -
static
127.0.0.1 - - [28/Apr/2024 22:56:34] "GET /static/favicon.png HTTP/1.1" 200 -
In your python code everytime your app recieves a request it will execute before_request()
that includes request for resources such as css, javascript and pretty much any request to your server.
You could simply add another condition so it only prints when the endpoint is not static
@app.before_request
def before_request():
print("before_request")
if 'username' not in session and request.endpoint != "static":
print(request.endpoint)