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.