D
D
Dim4os2016-05-24 20:10:57
Django
Dim4os, 2016-05-24 20:10:57

How to display an image in a template?

I can't seem to get the image into the template.

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

if settings.DEBUG:
#ошибку выводит типу: NameError: name 'patterns' is not defined
    urlpatterns += patterns(
        'django.views.static',
        (r'media/(?P<path>.*)',
        'serve',
        {'document_root': settings.MEDIA_ROOT}), )

#models.py
class ImageArticle(models.Model):
    imagearticle_image = models.ImageField(upload_to='article/', verbose_name='Фото')

#views.py
args = {}
args['image'] = ImageArticle.objects.all()
return render(request, 'article.html', args)

template:
{% for im in image.imagearticle_image %}
    	<img src="{{ im.url }}">
{% endfor %}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pan Propan, 2016-05-24
@mgis

args['image'] = ImageArticle.objects.all()
Here you get a list, not a specific model object, so you won't be able to get an image.
Pass one model instance to the context.

O
Oscar Django, 2016-05-25
@winordie

{% for im in image %}
  <img src="{{ im.imagearticle_image.url }}">
{% endfor %}

V
Vladislav Sklyar, 2016-05-26
@VladSkliar

Least

#urls

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question