Based on the first part of the path an app is accessed from, I want the app to render differently. I tried setting a variable on the request using middleware and then accessing it in the template. I printed some debug information, which did show up so I know the middleware code ran, but the value wasn't set once I got to the template. How can I set a variable inside middleware that can be used in Flask?
from werkzeug.wrappers import Request
class NavMiddleware:
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
request = Request(environ)
print('path: %s, url: %s' % (request.path, request.url))
if request.path.startswith("/myapp"):
print("Here")
request.application = "Myapp"
environ['application'] = "Myapp"
return self.app(environ, start_response)
{% if request.application == "Myapp" %}
{% include 'navbar_myapp.html' %}
{% endif %}