regan
regan

Reputation: 21

Nginx Static files for Flask Admin Page

I have trouble serving the static files for flask admin page.

admin = Admin(app, name='APPName', template_mode='bootstrap3') app.config['FLASK_ADMIN_SWATCH'] = 'Slate'

Everything seems to be working on localhost but when I upload the application to the server(ubuntu) the bootstrap template no longer works. The route to the static files for the Flask admin is http://127.0.0.1:5000/admin/static/bootstrap/bootstrap3/swatch/Slate/bootstrap.min.css?v=3.3.5 on localhost. On the server its https://domainname.com/admin/static/bootstrap/bootstrap3/swatch/Slate/bootstrap.min.css?v=3.3.5 but its returning error 404. Can anyone can point me to the right direction.

Upvotes: 0

Views: 587

Answers (1)

Strinnityk
Strinnityk

Reputation: 618

I know this probably comes late, but here:

Solution

Change your second line:

admin = Admin(app, name='APPName', template_mode='bootstrap3') 
app.config['FLASK_ADMIN_SWATCH'] = 'slate'  # This line: 'Slate' to 'slate'

Context

I was having the same issue you did. In my case, my config file contained this:

# Flask Config
FLASK_ADMIN_SWATCH: str = "Superhero"

Worked locally, but not when deployed to an EC2 machine running CentOS, I got a 404.

I was running Windows locally and a Linux-based remote machine. I bet you are as well. Windows is case-insensitive, while Linux is case-sensitive.

Having the FLASK_ADMIN_SWATCH variable set to Slate means the path that arrives at flask is something similar to admin/static/bootstrap/bootstrap4/Slate/bootstrap.min.css?v=4.2.1. This doesn't present an issue for Windows, but it does for Linux.

Swatch themes reside inside flask_admin/static/bootstrap/bootstrapX/swatch/, each of them on a folder of their own, and all of them lowercase (e.g.: cerulean, cosmo, etc.). Your specific theme directory is flask_admin/static/bootstrap/bootstrapX/swatch/slate/.

Windows will try parsing bootstrap/bootstrap4/Slate/bootstrap.min.css?v=4.2.1 and succeed at it, because it doesn't care for the case-ness of Slate, but Linux does, that's why it fails and ends up returning a 404.

# These paths are equivalent for Windows, but not for Linux.
bootstrap/bootstrap4/Slate/bootstrap.min.css?v=4.2.1
bootstrap/bootstrap4/slate/bootstrap.min.css?v=4.2.1

Upvotes: 4

Related Questions