How to render images on Django template

a 夏天 提交于 2019-12-01 13:43:32
Regis da Silva

urls.py

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from gallery.core.views import *
from django.contrib import admin

urlpatterns = patterns(
    'gallery.core.views',
    url(r'^$', 'home', name='home'),
    url(r'^gallery/$', GalleryList.as_view(), name='gallery_list'),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py

MEDIA_ROOT = BASE_DIR.child('media')
MEDIA_URL = '/media/'

STATIC_ROOT = BASE_DIR.child('staticfiles')
STATIC_URL = '/static/'

gallery_list.html

<html>
<body>
    <h1>Gallery</h1>

    {% if gallery %}
        {% for photo in gallery %}
            <p><img src="{{ photo.photo.url }}" width="300px"></p>
            <p>{{ photo.description }}</p>
        {% endfor %}
    {% endif %}

</body>
</html>

The secret is photo.photo.url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!