Home > Blockchain >  AWS ElasticBeanstalk Django display images
AWS ElasticBeanstalk Django display images

Time:04-28

i uploaded my Media directory which contains my Images but i cant access it to display my images. My images are correctly being uploaded when i send them, but i cant view them in the page

Im utilizing ElasticBeanstalk and RDS - Mysql

I only configured my RDS everything else like EC2, S3 are automattically configured by ElasticBeanstalk

commands I used on eb:

eb init
eb create
eb deploy

static-files.config:

 option_settings:
     aws:elasticbeanstalk:environment:proxy:staticfiles:
         /static: static
         /media: media

django.config:

 option_settings:
     aws:elasticbeanstalk:container:python:
         WSGIPath: store.wsgi:application

01_packages.config:

 packages:
     yum:
         python3-devel: []
         mariadb-devel: []

models.py:

class Product(models.Model):
    image = models.ImageField(upload_to='product_images/%Y/%m/%d')

settings.py:

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

my html:

                <a href="{{product.get_absolute_url}}">
                    <img  src="{{product.image.url}}}" alt="550x750">

                </a>

CodePudding user response:

If I do collectstatic again then the images will be deleted and i will need to upload again item by item

CodePudding user response:

You can try following configuration which worked for me.

settings.py

DEBUG = False
MEDIA_URL = '/media/'
MEDIA_ROOT = 'media'
STATIC_URL = '/static/'
STATIC_ROOT = 'static'

Now run python manage.py collectstatic

In root urls.py make following configuration

from django.conf.urls import url
from django.conf import settings
from django.views.static import serve 

urlpatterns = [
    ...
    ...
    url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}), 
    url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}), 
]

You can refer Github for better idea. Thank you

  • Related