Estoy tratando de mostrar una lista de imágenes en mi página de desarrollo, pero las únicas imágenes que obtengo son esta imagen en blanco .
Aquí están mis códigos de Django
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS =[os.path.join(BASE_DIR,"static")]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media_cdn')
views.py
def imgdownload(request):
allimages = ImagefieldModel.objects.all()
return render(request, 'main/show.html',{'images': allimages})
urls.py
urlpatterns = [
....
....
path("show/", views.imgdownload, name="imgdownload"),
....
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
modelos.py
class ImagefieldModel(models.Model):
title = models.CharField(max_length = 200)
img = models.ImageField(upload_to = "media")
class Meta:
db_table = "imageupload"
show.html
{% extends "main/header.html" %}
{% load static %}
{% block content %}
<head>
<title>Django Display Images</title>
</head>
<body>
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Image</th>
</tr>
</thead>
<tbody>
{% for img in images %}
<tr>
<td>{{img.title}}</td>
<td><img src="/{{ BASIC_DIR }}/{{img.img}}" width="120"/></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
{% endblock %}
No estoy seguro de cuál es el problema, ya que he declarado MEDIA_URL correctamente. Se agradece mucho algo de ayuda.
{% for img in images %}
<img src="{{ img.img.url }}" width="120"/>
{% endfor %}
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras