Question:
Hello, please help me with a problem.
I'm trying to make a blog in Django/Python , I want to display articles with pictures. The image is loaded by url from the admin, but not from the template, please tell me where to dig. Googled, Yandexil, found nothing working.
models.py:
class Article(models.Model):
title = models.CharField(max_length = 200)
text = models.TextField()
image = models.ImageField(blank=True)
def __unicode__(self):
return unicode(self.title)
urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('blog.urls')),
url(r'^media/(?P<path>.*)$','django.views.static.serve',
{'document_root':'media'}),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py:
...
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
article.html:
{% for article in articles %}
<h1>{{ article.title }}</h1>
<img src="{article.image}">
<p>{{ article.text}}</p>
{% endfor %}
(Tried article.image.url
crashes error)
Answer:
Least:
<img src="{{ article.image.url }}">
But not
<img src="{article.image}">
When using {{ article.image.url }}
, what error are you seeing?