本文介绍了如何在 Django 模板上渲染图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看我的 GitHub

See my GitHub

https://github.com/rg3915/gallery

如何在 Django 模板上渲染图像?但他回来了:

How to render images on Django template?But he is returning:

画廊

media/3665_1280x800_8UX8tgG.jpg

media/3665_1280x800_8UX8tgG.jpg

环法自行车赛

media/281405_fSRBDZu.jpg

media/281405_fSRBDZu.jpg

儿童自行车

如何渲染图像?

bug 是否在 settings.py 中

Does the bug is in settings.py

推荐答案

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>

秘密是photo.photo.url

这篇关于如何在 Django 模板上渲染图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 00:18