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!

On Wed, Jan 8, 2025 at 8:50 PM Szawunia <[email protected]> wrote:

> 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
>
> --
> 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 [email protected].
> To view this discussion visit
> https://groups.google.com/d/msgid/django-users/79227829-8ca1-40a1-8117-1732f1658207n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/79227829-8ca1-40a1-8117-1732f1658207n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Thanks and Regards

J. Ranga Bharath
cell: 9110334114

-- 
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-users/CAK5m316EiW9LH5HdLFEh%3DGcvo-xoirnDs9JpbZt5q%3DJTTbjFnw%40mail.gmail.com.

Reply via email to