Forums

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

Can we take a look at your files? We can see them from our admin interface, but we always ask for permission first.

Yes, you can, thank you

I don't see a directory called static in your app's directory on PythonAnywhere.

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: https://docs.djangoproject.com/en/3.1/ref/contrib/staticfiles/ https://docs.djangoproject.com/en/3.1/howto/static-files/deployment/

All the important links to the relevant docs were posted above. What is the point where you stop understanding? Giles told you that you do not have a static directory. Have you tried to create one?

What does it mean to create one? I have this directory on my local computer, but because of .gitignore, i suppose, it doesn't download on github, that is why I don't have this directory on pythonanywhere. Should I download static files by myself when i want to deploy my project?

Sorry, it sounds like @fjl misunderstood your question.

I think I see the source of the problem; you need to exclude the /static/ directory that is in the root of your project (the one that collectstatic puts files into), but right now it appears to be excluding all directories called static, including the one in your Django app, which is not what you want to happen. Is that right?

Normally if you put /static (note the leading slash) in your .gitignore, it will only exclude the one at the top level. Could you double-check what you have in the .gitignore on your local machine, and try running git status to see what it sees?

.gitignore: .pyc ~ pycache myvenv db.sqlite3 /static .DS_Store

git status: nothing to commit, working tree clean

I'm not sure how this process should go correctly. I've read that "If your build process includes running collectstatic, then you probably don't need or want to include staticfiles in your Git repository" (from gitignore.io). That's why i've add /static in my .gitignore.

At my local computer I have only one static folder. It's located in myapp folder (like I wrote in first post). When I use git push, there is no folder "static" in myapp directory on the github. It doesn't download on the github (because of .gitignore?). This is how it should be? If you were on my place, what order of actions would you take to download static files? I just can't understand, if we add /static (which ignores all static file transfers) in .gitignore how should we download static files to github and next to pythonanywhere? Thank you

Forgive me for wasting your time. I found what was the problem. Somehow the file was duplicated into the myapp folder. He had an empty name, but .gitignore type, maybe that's why I just didn't notice him all this time. So stupid. Thank you

no worries, thanks for letting us know!

Guys! It looks like it would be better to explain working with static files with some example. If a person sees an example project tree, its settings.py file and "Static files" at "Web app setup" it's much easier for them to understand what to do. I have the same issue and no site's tutorial works for me.

There are examples here https://help.pythonanywhere.com/pages/DebuggingStaticFiles/