0

I'm currently trying to deploy my project and I can't seem to get my static files working correctly like they did on my local environment using the collectstatic command. The service I am deploying on is pythonanywhere - this is where I'm at right now. Any help would be greatly appreciated!

settings.py

BASE_DIR = Path(__file__).resolve().parent.parent

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    BASE_DIR / "static",
    BASE_DIR/"static"/"images",
    BASE_DIR/"static"/"css"
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')

MEDIA_URL = '/images/'

LOGIN_REDIRECT_URL = 'home'

LOGIN_URL = 'login'

My file structure File structure

Pythonanywhere Path Pythonanywhere Path

2 Answers2

0

Try providing a STATIC_ROOT directory in your settings (this is where files will be collected by collectstatic, so you may have one already), eg,

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

Next, make sure that on the webpage tab your Directory value includes the name of this folder - in this case it's likely to be home/HFShippingLLC/mysite/static. This should essentially be the same value as static_root.

The URL value on the web tab isn't appended on to the directory value, which is where I think you might have come unstuck. Instead, it's an alias - so when the browser requests /static/ it looks in home/HFShippingLLC/mysite/static

There's a great explanation of the various STATIC settings in this question: Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT

NB: You should have DEBUG = False in your settings.py if you are wanting to use STATIC_ROOT, and making changes to settings in pythonanywhere usually requires a reload of the site via the web tab.

SamSparx
  • 4,629
  • 1
  • 4
  • 16
  • I got collectstatic to run and copy all my files to the designated location, changing my code in settings.py to this: STATIC_ROOT = os.path.join(BASE_DIR, "static") STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR/"static"/"images", BASE_DIR/"static"/"css" ] As well as the path on the website to this: home/HFShippingLLC/mysite/static However, nothing is able to be loaded from the static directory still, any ideas? – Kyle Feldman Jul 28 '22 at 23:46
  • Did you reload the website on the web tab? – SamSparx Jul 28 '22 at 23:52
  • Yep, the directory was not empty when I ran the collectstatic command - is that okay? – Kyle Feldman Jul 29 '22 at 00:02
  • Yes -that will happen if you've run it before or included it in an upload. Another thing to check is that you have DEBUG = False in your settings file for this environment. – SamSparx Jul 29 '22 at 00:05
  • I've had it set to True, should I change it to False? – Kyle Feldman Jul 29 '22 at 00:18
  • When I uploaded the static directory, I uploaded everything that I had on my local environment that was static related. To me - that seems like the common sensical thing to do but I read on that thread you linked that the directory should be empty and if you're using existing files they should be included in a different directory and added to STATICFILES_DIRS. Swapping debug to false presents me the same error. – Kyle Feldman Jul 29 '22 at 00:22
  • i.e GET https://hfshippingllc.pythonanywhere.com/static/images/register.png 404 (Not Found) – Kyle Feldman Jul 29 '22 at 00:23
  • Running the collectstatic command should go through all your apps and copy their /static folder contents and file structures (as well as any other folders you nominate in your STATICFILES_DIRS) into your STATIC_ROOT directory - basically so you don't have to. You can easily delete it and run collectstatic again, but the original /static directories in your app folders will need to still be there to be copied from. – SamSparx Jul 29 '22 at 00:36
0

I found my answer by making many changes, all of which were consistent with this answer found below - as well as this learn django guide which I will also link here:

Why does DEBUG=False setting make my django Static Files Access fail?

https://learndjango.com/tutorials/django-static-files