Image upload leads to Server Error (500) Django app deployed on railway.app

Question:

I have deployed a django app on railway.app. In development I can upload images but when I go to deployed app and click on upload it leads me to Server Error (500).

following is my settings.py code

STATIC_URL = 'static/'
# STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] 
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

if 'XXX' in os.environ:

    # Bucket config
    AWS_STORAGE_BUCKET_NAME = 'xxx'
    AWS_S3_REGION_NAME = 'xxx'
    AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
    AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
    AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'

    # Static and media files
    STATICFILES_STORAGE = 'custom_storages.StaticStorage'
    STATICFILES_LOCATION = 'static'
    DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'
    MEDIAFILES_LOCATION = 'media'

    # Override static and media URLs in production
    STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{STATICFILES_LOCATION}/'
    MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{MEDIAFILES_LOCATION}/'

html code

 <div>
      <img src="{{user_profile.profile_img.url}}" alt="Profile Image">   
 </div>
 
      <a href="{% url 'profile-image' user_profile.user user_profile.profile_uuid %}"</a>
Asked By: Sohail Shams

||

Answers:

I have solved this issue. I changed debug to true in production and which lead me to the the exact error which was actually in view logic.
I was getting absolute path of the image which was throwing this error so I changed this logic to get relative path which solved this issue.

Answered By: Sohail Shams
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.