This is the correct way to display image files using ImageField. Imagine we have a user profile profile:
models.py:
UserProfile: profilePic= models.ImageField( upload_to='PATH_TO_UPLOAD', blank=True, null=True)
settings.py:
MEDIA_ROOT = 'FULL_PATH_OF_MEDIA' MEDIA_URL = 'URL_OF_MEDIA'
urls.py:
urlpatterns = [ . . . ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
PATH_TO_UPLOAD is the path by which user data is loaded. This is a subdirectory of FULL_PATH_OF_MEDIA, which means that the downloaded file will have
FULL_PATH_OF_MEDIA/PATH_TO_UPLOAD
full path. Now this content can be obtained at this URL:
SITE_NAME/URL_OF_MEDIA/PATH_TO_UPLOAD
I also recommend reading this on static_files vs media_files
doc
Hojat modaresi
source share