0

I don't want to store images in static for this Django project because the user will be able to change them from the admin panel.

My code looks like:

<div class="col-md-3 col-sm-4 col-xs-6">
<img class="img-responsive" src="/galleryimg/first.jpg"/>
</div>

Where /galleryimg/ is a folder in the root directory of the project, and "first.jpg" is the file that I want to serve.

However, this does not load. How can I get it to load?

OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
Ethan Kershner
  • 121
  • 3
  • 13
  • 1
    You need to configure your media url, which is intended for exactly what you are trying to do here. See https://stackoverflow.com/questions/5517950/django-media-url-and-media-root – David Jul 09 '17 at 17:40

1 Answers1

3

Use a django media url

In your setting.py file

MEDIA_URL = "/Media/"
MEDIA_PATH = "Media"

In your urls.py file

from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [

]+ static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

Now create a Media folder in your root directory and then create galleryimg folder in Media folder.

Now use the following url in your template

<div class="col-md-3 col-sm-4 col-xs-6">
<img class="img-responsive" src="/Media/galleryimg/first.jpg"/>
</div>

Hope this helps you

NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45