Django:无法在模板中呈现上传的图像

时间:2018-11-02 03:02:13

标签: python django

我创建了一个模型,该模型可以在模型中使用FileField()接受任何类型的文件。

我已经正确上传了一些文件,并将引用保存在数据库中(如在管理界面中以及“媒体”文件夹中所见)。

但是,无法在模板中渲染此图像。我需要渲染它,这样我的用户才能将它们下载到他们的计算机上。

这些是我的图片的路径:

media/imagenes/468x60.jpg #relative path

/home/ogonzales/Escritorio/web_proyects/gallito/media/imagenes/468x60.jpg #full path

项目结构

gallito (Django project folder)
   |_gallito
   |_main_app
             |_static
             |_templates
                        |_main_app
                                  |_pedidos.html

   |_media
          |_imagenes
                    |_468x60.jpg
                    |_728x90.jpg
   |_templates
                |_registration
                              |_login.html
                |_base.html

models.py

class TamaniosCantidades(models.Model):
    TAMANIOS = (('2x2', '2" x 2"',), ('3x3', '3" x 3"',),
               ('4x4', '4" x 4"',), ('5x5', '5" x 5"',))

    CANTIDADES = (('50', '50',), ('100', '100',),
                ('150', '150',))


    tamanios = models.CharField(max_length=10, choices=TAMANIOS)
    cantidades = models.CharField(max_length=10, choices=CANTIDADES)
    imagenes = models.FileField(upload_to='imagenes/')
    uploaded_at = models.DateTimeField(auto_now_add=True)

forms.py:

class TamaniosCantidadesForm(forms.ModelForm):
    tamanios = forms.ChoiceField(choices=TAMANIOS, widget=forms.RadioSelect(), label='Selecciona un tamaño')
    cantidades = forms.ChoiceField(choices=CANTIDADES, widget=forms.RadioSelect(), label='Selecciona la cantidad')
    class Meta:
        model = TamaniosCantidades
        # fields = ['tamanios', 'cantidades',]
        fields = ['tamanios', 'cantidades', 'imagenes']

views.py:

def pedidos(request):
    pedidos = TamaniosCantidades.objects.all()
    return render(request, 'main_app/pedidos.html', {'pedidos': pedidos})

urls.py:

urlpatterns = [
    path('', views.index),
    path('productos/', views.productos),
    path('productos/die-cut-stickers', views.die_cut, name='die-cut-stickers'),
    path('post_url/', views.post_treasure, name='post_treasure'),
    path('post_url_tamanioscantidades/', views.post_tamanioscantidades, name='post_tamanioscantidades'),
    path('pedidos/', views.pedidos),
    # path('login/', views.login_view, name='login'),
    # path('logout/', views.logout, {'next_page': settings.LOGOUT_REDIRECT_URL}, name='logout'),
]

HTML:

<div>

        <p>Mi Imagen</p>
        <img src="/home/ogonzales/Escritorio/web_proyects/gallito/media/imagenes/468x60.jpg">


         {% for pedido in pedidos %}

            <p> {{ pedido.imagenes }}</p>
            <img src="{{ pedido.imagenes.url }}" alt="Smiley">

         {% endfor %}

</div>

更新1:

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

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

更新2:

这是呈现的html

<p> imagenes/728x90.jpg</p>
<img src="/media/imagenes/728x90.jpg" alt="Smiley">



<p> imagenes/468x60.jpg</p>
<img src="/media/imagenes/468x60.jpg" alt="Smiley">

1 个答案:

答案 0 :(得分:1)

请尝试在URLs.py中添加以下代码段:

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).

这应该使您的媒体在开发过程中能够在浏览器中工作。