0

I am using Django 3.1.

settings.py looks like

BASE_DIR = Path(__file__).resolve(strict=True).parent.parent MEDIA_ROOT = BASE_DIR / 'media' MEDIA_URL = '/media/' 

urls.py looks like

from django.urls import path, include from django.conf import settings from django.conf.urls.static import static urlpatterns = [ ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

my model image field looks like

dev_image_one = models.ImageField(upload_to='dev/', blank=True, null=True, verbose_name = 'image 1') 

When files are uploaded, they end up in the /media/dev directory.

When they are to be displayed, the url looks like:

<img src="dev/1.png"> 

If I manually append /media/ on to the front of the url, the image displays. I never had this problem before so I'm at a loss as to what is going wrong. I never used 3.1 before, so I'm wondering if that doesn't have something to do with it. No problem with the static files. Thanks.

1
  • please share the related template code Commented Sep 13, 2020 at 7:16

1 Answer 1

2

Make sure settings.py be like

STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / "static", ] MEDIA_ROOT = BASE_DIR / 'media' MEDIA_URL = '/media/' 

load {% load static %} in any page that you want to access image

use {% static 'images/india.jpg' %} to acess image

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.