Forums

Static file 404 with django 1.8?

In PythonAnywhere Web tab:

URL:     /static/
Path:    /home/username/project/static

In settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

...

STATIC_URL = "/static/"

STATIC_ROOT = os.path.join(BASE_DIR, "static")

STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'project/static'),
)

In template:

{% load staticfiles %}
<link type="text/css" href="{% static 'MyApp/style.css' %}" />
<link type="text/css" href="{% static 'project/style.css' %}" />
...
<script type="text/javascript" src="{% static 'MyApp/script.js' %}" ></script>

I run:

python manage.py collectstatic

which successfuly copies all static files into /home/username/project/static, however I get 404 errors for all static files. I am only having this error on PythonAnywhere. Everything runs fine on localhost.

The generated html and the 404 errors indicate the files are being searched for at the following locations:

http://username.pythonanywhere.com/static/MyApp/style.css
http://username.pythonanywhere.com/static/project/style.css
http://username.pythonanywhere.com/static/MyApp/script.js

This is what I expected, so I am assuming my misunderstanding lies in where these directories actually lie in PythonAnywhere.

I am very new to Django. See my question: http://stackoverflow.com/questions/30676929/how-do-you-link-to-site-wide-static-files-in-django-1-8" for a more detailed description of my static files structure.

P.S. I am using Django 1.8 in a virtualenv on PA, so please no deprecated/out of date answers.

TL;DR;

STATIC_ROOT = os.path.join(BASE_DIR, "static")

after running python manage.py collectstatic, all static files are successfully copied to `/home/username/project/static/

receive 404 errors when searching for static files at http://username.pythonanywhere.com/static/...

Did you hit the "Reload" button after you set up the static files on the web tab?

yep

oh. no. i didn't... embarressing.

Fixed me right up. thank you!

Excellent, glad I could help! :-)

Hi,

I deployed a working project in the domain 'aravind.pythonanywhere.com'. But the webpage loads without applying CSS styles.

For example, I can see from the page source that <link href="/static/css/bootstrap.min.css" rel="stylesheet"> is pointing to aravind.pythonaywhere.com/static/css/bootstrap.min.css. However, when I click the link I get not found error.

FYI, I have already ran the "python manage.py collectstatic" command. And all the .css file is available in the '/static/css/' folder. I also did reloading.

Could you please help me to figure out the issue?

Thank you!

Yours sincerely, Aravind Harikumar

you also need to setup the static files url from the webapps tab

Yes! The idea worked. Thank you!

But isn't it just a work around? I was wondering why it does not take the /static/ path mentioned in settings.py!

well it's not just django that uses static files. and you may want to serve additional static files. (eg: a quick webapp to make is one that just serves files of a particular folder- that could be useful just to share a file, or to make a js test suite public)

I have the same problem and already done all mention, but the problem is still here. Could someone help?

Have you worked through the help pages here and here. They describe some of the theory.

@conrad, I had this problem, but forgot to setup the static files url from the webapps tab, like you mentioned. Thanks.

Great! Glad you worked it out.