D
D
Dominik092015-09-13 17:46:50
Django
Dominik09, 2015-09-13 17:46:50

How to display an image in Django?

Unable to display image. It is written to the database. It is added to the folder with all the media files that I specified in settings.py.

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

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

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

However, checking through chrome, it is clear that the server cannot download them.
I did everything according to the instructions. Added to url.py
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings

urlpatterns = patterns('',
...
)
if settings.DEBUG:
    if settings.MEDIA_ROOT:
        urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()

Tell me what could I be missing?
Once again I say that the picture is loaded into the database, so the problem is definitely not in views.py.
Console:
b066be86a8ba49fc91adc817ae6f0b9a.png
Template:
<div class="container-fluid">
  <div class="row">
    {% for n in news %}
    <div class="col-lg-4">
        <img width="324" height="205" style=" margin-bottom: 3%;" src="{{ n.image.url }}">
      </div>
    {% endfor %}
  </div>
</div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2015-09-13
@sim3x

такая проверка там не нужна
покажи ошибку из консоли
покажи код темплейта

Владислав Скляр, 2015-09-13
@VladSkliar

Попробуй:
<img src="{{ news.image.url }}" />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question