1

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
  • What is `BASE_DIR` value? Doesn't `collectstatic` generate any text output about what's happened? – Ivan Starostin Apr 05 '21 at 19:51
  • BASE_DIR = Path(__file__).resolve().parent.parent 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. 132 static files it is admin static files. At my local server he collect 133 static file with an additional folder that contains the css file I need – Nikita Nikitov Apr 06 '21 at 04:33
  • Please update your question with additional details, don't comment. And take a look at this answer https://stackoverflow.com/questions/66437690/django-html-template-cant-find-static-css-and-js-files/66439076#66439076 – Ivan Starostin Apr 06 '21 at 04:49
  • I see that you are using different structure for css file location (I believe it's taken from PythonAnywhere help page?) and location where you run `collectstatic` management command. Just to avoid confusion: can you check what are the contents of the `/home/user/user.pythonanywhere.com/static` directory? – caseneuve Apr 06 '21 at 11:48
  • I suppose that the main problem is that I add the line /static in the .gitignore file? Is this why the pythonanywhere bash console doesn't see the static files? Because they haven't been download on github? Than what is the process of upload static files? Can you please explain me? I also read this articles, but still dont understand: docs/3.1/ref/contrib/staticfiles/ docs/3.1/howto/static-files/deployment/ @ivan-starostin i've read your answer. STATIC_URL - ok, STATIC_ROOT - ok. I dont need STATICFILES_DIRS, because default STATICFILES_FINDERS settings find static subdirectory of app. – Nikita Nikitov Apr 06 '21 at 15:51
  • `/static in the .gitignore` yes, static files in most cases are part of the project. If you don't generate them during build phase then they must be committed to git. – Ivan Starostin Apr 06 '21 at 17:57

4 Answers4

2

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.

caseneuve
  • 811
  • 4
  • 12
2

You should add the static files URL and directory in the web tab. A URL would be like/static/ and a directory would be like: /home/yourname/yourprojectname/static

Younes Belouche
  • 1,183
  • 6
  • 7
0

to display or load the static files you just need 2 steps:

  1. open project setting add a single line after STATIC_URL, paste the line STATIC_ROOT = '/home/QuizApp345/quiz_project/static' (it must be your project directory REPLACE MINE)
  2. in bash type python manage.py collectstatic
  3. add static file path in your web app setting in pythonanywhere. after these steps reload the web app your static files should be loaded now.
Asad Ali
  • 81
  • 1
  • 7
0

Static files configuration info for pythonanywhere can be found at the following link: https://help.pythonanywhere.com/pages/DjangoStaticFiles.

That said, I had the same issue mentioned in this thread even though I did all the setup correctly. The issue was related to a pythonanywhere specific configuration: in the Web tab (static files section) you should enter use the following values.

  • URL /static/
  • Directory /home/your-username/path/to/your/proyect/static

The tricky thing is that the directory also needed to include the path "/static"

Javi
  • 1
  • 1