I have this folder structure for my flask app:
app.py
/templates
login.html
index.html
/static
/css
main.css
login.css
/js
login.js
/images
image.png
When I use <link rel="stylesheet" href={{url_for('static',filename='css/main.css')}}>
inside login.html, it renders as
<link rel="stylesheet" href="/static/css/main.css">
(Note the prepended slash before static).
Now, this leads to 404 errors because I intend to run to run this application with /app1/ in the NGINX server block. Due to the prepended slash, the application tries to find main.css in XXX.XXX.XX.XX/static/css/main.css
rather than XXX.XXX.XX.XX/app1/static/css/main.css
If I do not use url_for() and write the entire file path inside login.html i.e. <link rel="stylesheet" href="static/css/main.css">
(without prepending the slash before static), it works just fine.
But I want to use url_for() inside the templates, as it is considered a good practice and saves a lot of trouble.
Any help/suggestion would be highly appreciated.
[EDIT]: The existing questions ask about "adding a prefix" to url_for(). This question pertains to understanding why url_for() is prepending "/" in its route, and how to remove the prepended slash.The application is working if I write the entire url path in the template, but that is not a good solution.