Django Static Files does not deploy on Google Cloud Bucket - django

I am deploying an Django App, which runs on google server but it does not load the static files, so that App does not load the css and images.
I checked the bucket I created but there is no files in the bucket.
Here is settings.py
STATIC_URL = '/static/'
STATIC_ROOT = "static"
PRODUCTION = True
if PRODUCTION:
# Define static storage via django-storages[google]
GS_BUCKET_NAME = env("GS_BUCKET_NAME")
STATIC_URL = "/static/"
DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
GS_DEFAULT_ACL = "publicRead"
else:
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
Here is app.yaml file.
runtime: python38
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
Here is urls.py.
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
When I deploy the app, it was done successfully. but the static files does not deploy into the bucket.
As well I run this bellow command.
python manage.py collectstatic
I followed this tutorial.
Here is structure of static directory.

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

Why is collectstatic only detecting admin static files?

I'm using S3 to store my static files, but I'm having an issue where only the admin static files are uploading to the bucket.
I expected to see the css and other folders from inside static upload to the bucket, but instead, this is all I got:
static/
- admin/
- flags/
What can I do to get my app's static folder to upload also?
Looking at other answers to this question, it seems like the main difference is that my project uses django-sass-processor, but I'm not sure why that would mean my entire folder is not picked up.
Project folder structure:
backend/
- accounts/
- core/
-- settings.py
-- ...
- static/
- templates/
settings.py
USE_S3 = os.getenv('USE_S3') == 'TRUE'
if USE_S3:
# aws settings
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
AWS_DEFAULT_ACL = 'public-read'
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
# s3 static settings
AWS_LOCATION = 'static'
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{AWS_LOCATION}/'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
else:
STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'sass_processor.finders.CssFinder',
]
SASS_PROCESSOR_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Django - Run collectstatic to collect static files but collect all the files in the project directory instead

I am trying to run collectstatic command to collect all of the my static files from the subdirectories of apps and deployed them to my Amazon S3 buckets.
I tried to use Zappa following the steps as found in this link: https://romandc.com/zappa-django-guide/walk_static/
After running the command python /manage.py collectstatic, all of the files in my project directory get copied into the static folder. Plus, it is not deployed into the S3 bucket.
Here is my code:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
# os.path.join(BASE_DIR, "/static"),
]
if os.environ.get("collectstatic")=='True':
# STATICFILES_DIRS += 'static',
STATIC_ROOT = os.path.join(BASE_DIR, 'test')
#STATIC FILE SETTNGS FOR S3
#Used when we seperate css and js into a static folder
DEFAULT_FILE_STORAGE = "django_s3_storage.storage.S3Storage"
if os.environ.get('stage'):
STATICFILES_STORAGE = "django_s3_storage.storage.StaticS3Storage"
AWS_S3_BUCKET_NAME_STATIC = 'static-myapp-'+ os.environ.get('stage')
# These next two lines will serve the static files directly from the s3 bucket
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_S3_BUCKET_NAME_STATIC
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
else:
STATICFILES_DIRS+=BASE_DIR,
# AWS BUCKET FOR UPLOADED FILES
AWS_S3_UPLOAD_BUCKET = "myapp-uploads"
Can anyone point out what I did wrong please? Thank you!

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.

Django. Alternate between local & remote staticfiles

After collecting my staticfiles and storing them in an Amazon Bucket (AWS S3), when I run the project locally it still uses the staticfiles stored online, this is a problem cause when I want to make a change on a css file for ex, I have to run collectstatic or manually upload the file to Amazon. I tried adding a new setting variable "LOCAL_STATICFILES" like this:
settings.py
LOCAL_STATICFILES = False
if not LOCAL_STATICFILES:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = os.environ['AWSAccessKeyId']
AWS_SECRET_ACCESS_KEY = os.environ['AWSSecretKey']
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
S3_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = S3_URL
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
if LOCAL_STATICFILES:
STATIC_URL = '/static/'
STATIC_ROOT = '/'
But when I turn LOCAL_STATICFILES to True and runserver, django can't find them.
The project's folders look like this:
project
app
app
static
css
js
img
templates
What am I doing wrong?
First of all: Ensure you have a way to distinguish whether you are, or not, in an environment supporting the Amazon bucket configuration. This means, usually this will be your production environment, where you already configured the amazon bucket settings.
So you will:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# this is the base project path
if 'AWSAccessKeyId' in os.environ:
# assume the presence of this key will determine whether
# we are, or not, in the bucket-supporting environment
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = os.environ['AWSAccessKeyId']
AWS_SECRET_ACCESS_KEY = os.environ['AWSSecretKey']
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
STATIC_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
# static url will be the re
STATIC_ROOT = None
# STATIC_ROOT doesn't matter since you will not invoke
# `manage.py collectstatic` from this environment. You
# can safely let it to None, or anything like:
# os.path.join(BASE_DIR, 'static')
else:
STATIC_URL = '/static/'
# the static files url will point to your local,
# development, server
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# here, STATIC_ROOT matters, since you will invoke
# `manage.py collectstatic` from your local environment.