Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pythonanywhere django static files are not collected from application

The problem is that static files from Django app are not being collected in pythonanywhere. After the command

python manage.py collectstatic

In the directory

/home/user/user.pythonanywhere.com/static

Only the admin folder appears. settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp'
]

Css file location:

+---myapp
|   +---static
|   |   \---myapp
|   |       \---css
|   |           \---myapp.css

When I run the command python manage.py collectstatic in my local command line, static files from the application are collected, the problem is in pythonanywhere.

All actions are standard, I did everything strictly according to the guide, I cannot understand what's the matter. Thanks a lot

I have read all the articles on this topic on pythonanywhere and everything I found on stackoverflow and in the documentation, but nothing helps to solve the problem.

https://help.pythonanywhere.com/pages/DjangoStaticFiles
https://help.pythonanywhere.com/pages/DebuggingStaticFiles/
https://help.pythonanywhere.com/pages/StaticFiles

UPD: BASE_DIR = Path(file).resolve().parent.parent

Bash console text output:

You have requested to collect static files at the destination location as specified in your settings: /home/user/user.pythonanywhere.com/static This will overwrite existing files! Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: yes 0 static files copied to '/home/user/user.pythonanywhere.com/static', 132 unmodified. 

The contents of /home/user/user.pythonanywhere.com/static directory after collectstatic command:

+---admin
|   +---css
|   +---fonts
|   +---img
|   +---js
like image 237
Nikita Nikitov Avatar asked Mar 11 '26 17:03

Nikita Nikitov


1 Answers

On PythonAnywhere, apart from the regular Django setup for static files, you need to perform an extra step of setting up static files mappings on the Web page (config page for your web app).

  • Go to the Web tab on the PythonAnywhere dashboard
  • Go to the Static Files section
  • Enter the same URL as STATIC_URL in the url section (typically, /static/)
  • Enter the path from STATIC_ROOT into the path section (the full path, including /home/username/etc)
  • Then hit Reload and test your static file mapping by going to retrieve a known static file.

As a sanity check, for example, if you have a file at /home/myusername/myproject/static/css/base.css, go visit http://www.your-domain.com/static/css/base.css.

See the docs for more details.

like image 125
caseneuve Avatar answered Mar 13 '26 06:03

caseneuve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!