Django isn't loading static admin files - django

when I access to my admin dashboard in Django, it doesn't load css and img files, so doesn't work well.
All the files are in 'project/static/admin', and in my settins.py I have this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
When I execute collectstatic, appears: 0 static files copied to '/opt/myenv/myenv/static', 318 unmodified.
I'm using Nginx + Gunicorn, also I'm using https with Cloudflare.
Can anyone help me?
Thanks.

You run collectstatic with STATIC_ROOT set up
If you do not have this in your settings.py , add it
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]

Related

All static files returning 404 error when deploying Django Project using Digital Ocean

I have tested my app in development and successfully got it fully functional in development running on my local server. I have now been trying to push it into production and none of the static files are being served. The connection has been successful as the domain shows the app with just plain HTML. However, all static files are returning the same 404 error.
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')
EDIT: installing whitenoise fixed this!
try this :
urls.py
from django.conf.urls.static import static
urlpatterns = [
...
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Or just try this command:
python manage.py collectstatic
try this on top every HTML file if you didn't:
{% load static %}
this is because when you add some static files then you must tell manually the template to load those static file.
for more you can see here

Django static files not loading properly

I tried to get my project set up with static files and I do not seem to be successful. I want a global static files instead of an in-app one and so my settings.py is
STATIC_URL = '/static/'
STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
And as such my directory is
The issue I face is that my vendor folders will not load. Only images do and my custom css file.
What I did is move animate.min.css to the css folder and it worked but I wonder if being inside many folders affects it.
root urls.py
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] # It will be deactivated when running collectstatic. Active on localhost
#STATIC_ROOT = os.path.join(BASE_DIR, 'static/') # It will be activated when running collectstatic. Active on server
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Deployed Django project does not load static files

I have just deployed (first time ever) a Django project to a web server. Everything (including postgres) works flawlessly, except that static images won't load.
Here is the relevant part of the settings.py:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
#STATICFILES_DIRS = [
# os.path.join(BASE_DIR, "static"),
#]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_REDIRECT_URL = 'home'
LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
AUTH_USER_MODEL = 'account.CustomUser'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
The error message I get is the following in the console:
"GET /static/images/logo.png HTTP/1.1" 404 1808
However, when checking the file path in the console I have the following:
root#melius:/django/melius/static/images#
In this folder the files are present.
Where is my mistake?
There can be two reaon your static files not working.
First of all, Django doesn't serve static files on production.
Also i notice your static settings are wrongly configured.
Make sure your setting.py is look like this:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'YOUR STATIC FILE FOLDERS')]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
If you run your application on Nginx or apache2please make sure you configured the.conf` file properly to serve the static files.
Also, there is a straightforward and easy solution, that is whitenoise
If you don't want to serve your static files with Nginx or a similar server.
You can try whitenoise
To use whitenoise, please install it first and update your settings.py file like this:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
and add this in middlware before the sessionmiddleware
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # Serve static in production without nginx or apache
'django.contrib.sessions.middleware.SessionMiddleware',
........
]
Hope the solution will solve your problem
Here you go for whitenoise docs: http://whitenoise.evans.io/en/stable/
Note: static root and static dirs shound't be same

django return 404 uploading media file

hi I have a problem when I upload a file to the deployed server. It returns 404 does not matter if I do it by admin or by a view but if I can see the files that are already on the server.
I am hosting my app in namecheap on a shareserver and the settings are
settings.py
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_ROOT = os.path.join(BASE_DIR, 'static-server')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
# Media folder for database media
MEDIA_URL = '/static-server/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/static-server/media')
url.py
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT, show_indexes=True)
switch between debug mode true or false on local and it works perfectly but not when I deploy ... if anyone has any ideas. Well, from namecheap support they answered that the error is not on their side
Django does not serve media or static files in production as it just means to save the link for it. You need to use other services as Amazon Web Services S3 Bucket to serve it. But if you want to serve it anyway you can do in using Whitenoise.
First pip install whitenoise and then pip freeze > requirements.txt
Then in settings.py
MIDDLEWARE = [
# 'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
then add this line
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
then push this code to your production server and make sure you run collectstatic command i.e python manage.py collectstatic your static files now should be working.

Error deploying Django, unable to configure static files

I tried deploying Django project to www.pythonanywhere.com, My website ran but could not load static files.
I did collectstatic command in hosting server console but got error.
my project file configuration as follows
myproject # parent directory
-DjangoApp1 # an app inside project
-Myproject main file(includes manage.py etc)
-DjangoApp2 # an app inside project
-DjangoApp3 (in this app my static files located `DjangoApp3/static/DjangoApp3` )
in settings.py file
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = ('/DjangoApp3/static/DjangoApp3/',)
STATIC_URL = '/static/'
when I run collectstatic method in local machine ,below error occured
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\DjangoApp3\\static'
This works
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'staticfiles'),)
STATIC_URL = '/static/'
I've covered the three in an answer here before
Try this:
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
(os.path.join(BASE_DIR, 'static')),
)