How to Find Django ImageField URL

This setup is working for me, maybe it will help you. It is for latest version of Django. Many answers in OS are for older Django versions.

URLS:

from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
#url
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Settings:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Template:

<img src="{{ foo.image.url }}"><br>

Model:

image = models.ImageField(upload_to = 'img/', default = 'img/None/no-img.jpg')

My foo model has an imagefield, when it is stored, I can retrieve the full url through item.image.url based on the above setup.

posted @ 2023-06-02 16:15  Oops!#  阅读(60)  评论(0编辑  收藏  举报