Forums

Staticfiles still not loading

I think I followed https://docs.djangoproject.com/en/1.6/howto/static-files/ I also tried Debug=False, but I still cannot get static files, the image file set in template showed on the page... I shared my project at github, please help to file what's the reason

git clone https://github.com/viewplatgh/energybean.git energybean

Thank you!

In addition, I found the static files works in my local environment, but just not on pythonanywhere, please see "http://energybean.pythonanywhere.com/feedback2013/feedback2/", the test image on it not showing.

Ok, I figured it out. I added a custom static file url into the 'Static files' section in 'Web' tab. It works now. But not sure the nitty-gritties of how it works...

Sorry, I still have issues. The image file is loaded successfully, but javascript source doesn't. Javascript can work on my local environment, not working at pythonanywhere url, can you please help this? Thanks!

Here is a complete work around for you troubles. Instead of hosting your static files (css, js & images) in pythonanywhere, put them in Google Drive or Dropbox or even better Amazon S3. Make the files public when you upload them and you will get a link to each file. Use that link in all of your html files. Now, all the static files are served from external source which will greatly reduce the load on your instance(?) and will also definitely reduce page-load times. This, in my opinion, is much faster and easier. Note that you will still set debug to false.

Hmm, static files certainly should work on PythonAnywhere. And while vivekannan is right that you can serve them externally, it really doesn't save much -- if you use the static files part of the web tab, they're served up using a very efficient process in PythonAnywhere and it doesn't eat up your resources at all. (Using the Django DEBUG=True option is, however, quite inefficient.)

@energybean -- could you give us an example URL of a static file that doesn't work?

@giles The URL I gave in a previous post, http://energybean.pythonanywhere.com/feedback2013/feedback2/ I am using javascript CDN now, instead of using the one I uploaded to the pythonanywhere's static folder. Static image files work, it's good enough. Thanks.

On that page, I can see jquery-1.10.2.min.js loading from PythonAnywhere in /static/feedback2013 and I see a request for jquery-1.10.2.min.map that 404s because that file doesn't exist.

I'm having similar issues. I've got just a simple css file that i need loaded, which worked from my quickstart django install (1.3.7), but now doesn't work with my 1.6 install on virtenv.

settings.py has STATIC_URL = '/static/'

and the static folder has been placed in both the project root and the app folder (with reloads between each).

If you hit meteorainer.pythonanywhere.com you will see it shows /static/main.css but it's never found.

Does PA require the external implementation from the web tab as opposed to the framework supplied ones?

@meteorainer, you have no static files mappings except for the Django admin ones on /static/admin. You need to specify a static file mapping for /static that points to a folder that contains your static files if you want to have them served there.

Ok. So it is required that i have both a mapping in Django and in PA. Still getting used to all the differences between frameworks and raw hosting.

~EDIT~ Got it going now. Thanks :)

As an additional note, when I've been updating my static files (js/css) the changes aren't reflected instantly. I haven't timed the delay, but it's at least 5 mins for me. So don't panic if the changes aren't reflected. But definitely make sure you've added the static directories on the "Web" tab of your Python Anywhere admin page.

That's very strange! It should be pretty much instant -- certainly no more than 30 seconds. Just to make sure I'm understanding you correctly -- you have your static file mappings set up on the "Web" tab, and all you're doing is changing the CSS/JS in the directories that those mappings point to. But the changes you make can take 5 minutes to come through.

Is that right?

One possibility -- when we serve up a static file, we do caching for you. If a browser sends a message saying "only send me this file if it hasn't changed since June 15, 2017 at 15:08 UTC" and the file hasn't changed since then, our server sends back a message saying "all OK, it hasn't changed". This means that if the clock on your own machine is different to the clock on our servers (no need to worry about timezones, that's all handled automatically) then you might get cached pages back.

A way to check if this is the case -- if you run the "date" command in a bash console, does it match up with the date/time on your machine? (Allowing for timezones -- we're on UTC, you're probably not.)

A way to work around the issue (which might also be a useful way to check) -- if you hold down the shift key while reloading a web page in your browser, that means that the page will be reloaded ignoring all caches, so it might be more reliable if this is a cache problem.

Yup you were right about the caching - thanks! Yesterday my JS file wasn't initially working when I did collectstatic and pulled it from PA. It was working when I got home after work (so I assumed latency) but my CSS file wasn't. After reading yoru comment, I cleared my cache and it's all good now. Thanks!

Yup you were right about the caching - thanks! Yesterday my JS file wasn't initially working when I did collectstatic and pulled it from PA. It was working when I got home after work (so I assumed latency) but my CSS file wasn't. After reading your comment, I cleared my cache and it's all good now. Thanks!

No problem -- glad I could help :-)

Hi,

I'm trying to build one flask-based application using the pythonanywhere.com website but when I try to apply external CSS it's not working at all and always showing me the response in pink background-color which I haven't applied in my code. I tried to figure it out the reason behind it but unable to understand. This is my code snippet please guide me where I am doing wrong!

My CSS CODE: /home/rajnishjha/mysite/static/mystyle.css

/home/rajnishjha/mysite/static/mystyle.css

body { background-color: orange; }

My HTML CODE: /home/rajnishjha/mysite/templates/home.html

<html> <head> <!--This is a way to link or apply css file to my HTML code--> <link rel="stylesheet" href="../static/mystyle.css"> </head> <body bgcolor="yellow"> <h1>Hello Rajnish!!</h1> </body> </html>

MY Backend code: /home/rajnishjha/mysite/flask_app.py

from flask import Flask from flask import render_template

app = Flask(name)

@app.route('/') def hello_world(): return render_template('home.html') #return 'Hello from flask!'

@app.route('/name') def demo(): return 'Rajnish'

@app.route('/city') def demo2(): return 'Bangalore'

what do you mean by external css?

you set the bgcolor twice in your code as well.