Answer the question
In order to leave comments, you need to log in
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)
{% for im in image.imagearticle_image %}
<img src="{{ im.url }}">
{% endfor %}
Answer the question
In order to leave comments, you need to log in
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.
{% for im in image %}
<img src="{{ im.imagearticle_image.url }}">
{% endfor %}
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 questionAsk a Question
731 491 924 answers to any question