Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Problem with displaying media files

46 views
Skip to first unread message

Szawunia

unread,
Jan 8, 2025, 10:21:08 AMJan 8
to Django users
Hi,
I completely stuck with displaying media files - 'ImageField' in my html. Static files are displayed ok. I've read the 'whole internet' that topic and still nothing. I'm working on mac OS Sonoma, Django 5.1.3, Python 3.11.

My Html:
<img class="fit-picturePr" src="{{miod.miody_foto.url}}">

My model:
miody_foto = models.ImageField(upload_to = 'miody_foto', blank=True, null=True, default='miody_foto/spadz.jpg')

view:
def miody(request):

mymiody = Miody.objects.all().values()

template = loader.get_template('Produkty/miody.html')

context = {

'mymiody': mymiody,

}

return HttpResponse(template.render(context, request))


Projects url:

urlpatterns = [

path('', include('Pasieka.urls')),

path('', include('Galeria.urls')),

path('', include('Produkty.urls')),

path('admin/', admin.site.urls),

]

if settings.DEBUG:

urlpatterns += static(settings.MEDIA_URL,

document_root=settings.MEDIA_ROOT)


Application:

urlpatterns = [

path('Produkty/miody/', views.miody, name='miody'),

path('miody/', views.miody, name='miody'),

]


Additionally, files are displaying properly in my admin:

class MiodyAdmin(admin.ModelAdmin):

def image_tag(self, obj):

return format_html('<img src="{}" style="max-width:200px; max-height:200px"/>'.format(obj.miody_foto.url)) # wyswietlane zdj miodu

image_tag.short_description = 'miody_foto'

list_display = ("rodzaj", "opis", "miody_foto", 'image_tag',)# 'miody_foto.url', 'miody_foto.height', 'miody_foto.name',)

admin.site.register(Miody, MiodyAdmin)#, ImageAdmin)


Any help would be priceless.


helpless Ewa

RANGA BHARATH JINKA

unread,
Jan 8, 2025, 11:53:14 AMJan 8
to django...@googlegroups.com

Hi,

The issue is with how you're passing the mymiody data to the template. In your view, you are using:

mymiody = Miody.objects.all().values()

The .values() method returns dictionaries instead of model instances, which means you lose the ability to call .url on the ImageField.

Solution:

Change your view to:

def miody(request):
    mymiody = Miody.objects.all()  # Removed `.values()` to pass model instances
    template = loader.get_template('Produkty/miody.html')
    context = {
        'mymiody': mymiody,
    }
    return HttpResponse(template.render(context, request))

Template Fix:

Loop through the queryset correctly in your HTML:

{% for miod in mymiody %}
    <img class="fit-picturePr" src="{{ miod.miody_foto.url }}" alt="{{ miod.rodzaj }}">
{% endfor %}

Check Your settings.py:

Ensure your media settings are correctly defined:

import os

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

Make Sure to Serve Media Files (for local development):

Ensure your urls.py includes:

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

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This setup should display your media files correctly. Let me know if you need further help!


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/79227829-8ca1-40a1-8117-1732f1658207n%40googlegroups.com.


--
Thanks and Regards

J. Ranga Bharath
cell: 9110334114

Ewa Adamus

unread,
Jan 8, 2025, 1:45:32 PMJan 8
to django...@googlegroups.com
Hi,
You’re right, the problem was with passing data to the template in my view. It is working now!!!

Thank you very much!!!!

I should send you my best honey!

Best regards to you

Ewa

Reply all
Reply to author
Forward
0 new messages