znawca
znawca

Reputation: 279

How to display images in Django 1.10

i am not good in django, i use Django 1.10 and now i have problem with image displaying. I read in this version some stuff has changed but i don't get it. Here is what i have now:

settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

TEMPLATE_DIR = os.path.join(BASE_DIR, '/templates')

STATICFILES_DIRS = [STATIC_DIR, ]

STATIC_URL = '/static/'

MEDIA_ROOT = 'C:/Users/john/myprojects/goal/website/goal/media/'

MEDIA_URL = '/media/'

Maybe something is unusual here and i can delete it?

template:

{% block content%}
    {{ car.name }}
    {{ car.photo.url }}
{% endblock %}

Here is folder where i have my images:

C:/Users/john/myprojects/goal/website/goal/media/images

And what i see instead of image on my website:

/media/images/mynewcar.jpg

What must i change?

i pasted to urls.py:

media_dir = os.path.join(os.path.dirname(file),'media')

and to urlpatterns

url(r'^media/(.*)$','django.views.static.serve',{'document_root': media_dir}),

Upvotes: 0

Views: 805

Answers (1)

itzMEonTV
itzMEonTV

Reputation: 20339

You have to put <img> tag

{% block content%}
    {{ car.name }}
    <img src="{{ car.photo.url }}">
{% endblock %}

Update

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

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Upvotes: 4

Related Questions