2015-04-01 65 views

回答

4

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

+0

謝謝,謝謝,謝謝!我一直在研究這個問題超過一個小時,而你的問題已經解決了我的問題(並且很快)。 – allardbrain 2016-06-09 19:03:21