Forums

Flask: Css not Loading + Templates no routing

Hii.. my username is :nathumang . I have uploaded a flask_app which works perfectly on pycharm on my system but on hosting is not connecting the Css and also not routing to the pages it is supposed to properly. Any help is appreciated!:)

Are you using absolute paths (like /home/myusername/myapp/mystuff, not just myapp/mystuff) everywhere?

Re: CSS, do you need to set up a static url (on the web tab), or do you expect flask to be serving static files automatically?

using absolute paths.. the static url's are set up on the web tab already.. Im using the url_for module to import the css files to the html templates. The module is being imported in the main .py file which pythonAnywhere's console is not letting me import marking it as unused.. can't figure out a way to get around that!:(

I just checked out nathumang.pythonanywhere.com and it looks like some of the URLs for your stylesheets aren't being expanded by the template engine:

<link rel="stylesheet" type="text/css" href="{{url_for('/home/nathumang/mysite/static',filename='style.css')}}" />

Is that something you've investigated?

hmm.. yes.. that is the problem.. but they same works on my local machine.. how to solve this?

It does look a bit strange, though -- isn't the first parameter of url_for meant to be a function? Is the code with the unexpanded url_for call exactly what's in the template? Or is Flask/Jinja2 doing something to it before serving it up on the website?

okay.. so really appreciate all ur effort @giles.. but here is a weird situation.. I have 3 html pages.. all linked to the same css file using the same way as shown above. The two pages which can be navigated to from the home page work fine with the CSS linked while the index pae does not show any CSS.. although it is linked to the same file in the same way. Thi si very baffling to me and after doing a lot of debugging, I am stuck here.. can u please help me out with this!

That is very strange.

One thing to check -- are you using the same version of flask on your own PC as on PythonAnywhere? If not, you might want to use a virtualenv in order to upgrade to the latest version, on pythonanywhere. Instructions here: https://www.pythonanywhere.com/wiki/Virtualenvs

Having same problem. Renamed the CSS file and changed its name in my layout template on the url_for line and it works. If I change things back it goes to an earlier version of the CSS file. Guessing it is cached somewhere but haven't found out where yet. I did remove the app pyc file.

Still only works if I use a different name for the CSS file. Maybe tomorrow the cache (if that's what it is) will be flushed.

that's weird. out of curiosity, what was the original name of your css file?

main.css -- And its working fine this morning. Must be a caching issue. Just not sure where the cache issue is. Next time I have it happen I will check with another computer and/or browser to see if it is a client side issue. ;-)

By the way Conrad, thank you for your interest.

One thing (just in case you don't already know about it) -- you can generally do a force-refresh on a page in your browser by holding down shift while you click the "reload" button. That often clears up problems like this.

Sorry if that's something you're already fully aware of, just thought it would be worth mentioning :-)

Forced refresh: [holding down shift while you click the "reload" button] OMG ( Its so obvious now) I finally found this and it worked ... My suggestion is to add the requirement ( or at least note) for the user to do a forced refresh when making changes to static files on the WEB tab.

:-) It's not something we can control automatically, but yes, maybe we could add a note -- I'll see if there's anywhere we could fit that, though of course the page is already a little crowded so it might not be possible...