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
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
.
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))
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 %}
settings.py
:Ensure your media settings are correctly defined:
import os
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
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.
To view this discussion visit https://groups.google.com/d/msgid/django-users/CAK5m316EiW9LH5HdLFEh%3DGcvo-xoirnDs9JpbZt5q%3DJTTbjFnw%40mail.gmail.com.