I have been following this video on Youtube: https://www.youtube.com/watch?v=inQyZ7zFMHM1
My project so far is working fine with static files and all the files load and work properly. So, now I have to deploy the website on Heroku and for that, I uploaded the database on Amazon AWS using this video.
After bucket creation, I did the configurations as mentioned in the video (copied the static files into the Bucket AWS) but it didn’t work for me. It always showed me an error, that failed to load the resources
On searching, I found the command python manage.py collectstatic
to upload files into Bucket so I tried it out but this is the error I keep on getting
TypeError: sequence item 0: expected str instance, NoneType found
I have searched a lot on this but unable to figure out what is the issue. This is also the same error which I got when I myself uploaded static files into Bucket and tried to upload a Profile Image
My settings.py
file is as follows,
"""
Django settings for fcap project.
Generated by 'django-admin startproject' using Django 4.1.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
from pathlib import Path
import os
from django.contrib.messages import constants as messages
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-(c&774q+skzfb=n499li8!!!jv_m&s65eli3#w&zl+%c8h%sb$'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
MESSAGE_TAGS = {
messages.DEBUG: 'alert-info',
messages.INFO: 'alert-info',
messages.SUCCESS: 'alert-success',
messages.WARNING: 'alert-warning',
messages.ERROR: 'alert-danger',
}
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fcap_users',
'rest_framework',
'storages',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'fcap.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'fcap.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'fcap_db',
'USER': 'asad_hussain',
'PASSWORD': '(hidden for purpose)',
'HOST': 'database-2.cfhvhdusomqt.us-east-1.rds.amazonaws.com',
'PORT': '5432',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'fcap/static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = "/images/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# S3 Bucket Config
AWS_KEY_ACCESS_KEY_ID = '(hidden)'
AWS_SECRET_ACCESS_KEY = '(hidden)'
AWS_STORAGE_BUCKET_NAME = 'asad-hussain-fcap'
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_S3_ADDRESSING_STYLE = "virtual"
This is my requirements.txt
asgiref==3.5.2
boto3==1.24.50
botocore==1.27.50
Django==4.1
django-filter==22.1
django-storages==1.13.1
djangorestframework==3.13.1
jmespath==1.0.1
Markdown==3.4.1
multielo==0.4.1
numpy==1.23.1
pandas==1.4.3
Pillow==9.2.0
psycopg2==2.9.3
python-dateutil==2.8.2
pytz==2022.1
s3transfer==0.6.0
six==1.16.0
sqlparse==0.4.2
tzdata==2022.1
urllib3==1.26.11
validate-email==1.3
My File Structure is as follows,
All the static files are working fine if they are on localhost but not working after the connection with AWS. However, the Postgres database is uploaded on AWS and it is working fine (that is, I can create users and etc using the AWS Database and they work fine.)
2
Answers
Comment out the ‘STATICFILES_DIRS’ and try again. That was only used in my development.
also on a side note, i would remove the access keys from your bucket config when posting here. Even in production, they shouldn’t be seen there. You can use a JSON config file or another method thats not accessable.
You have mispelled
AWS_KEY_ACCESS_KEY_ID
It should be
AWS_ACCESS_KEY_ID
The program expects a string against
AWS_ACCESS_KEY_ID
but because it is mispelled, so it is gettingNone
instead. Due to this reason, you are getting an error thatNoneType Found