テンプレートに画像を表示する方法は?
私はこのようなことを試みます(しかしうまくいきません):
これは私のモデルです:
from django.db import models
class Photo(models.Model):
title = models.CharField(max_length=255)
img = models.ImageField(upload_to="media/galery")
class Meta:
verbose_name = "Galery"
verbose_name_plural = "Galery"
def __unicode__(self):
return self.title
views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from kolejny.galery.models import Photo
def photo(request):
photos = Photo.objects.all()
return render_to_response('photo.html', {'photos' : photos}, context_instance=RequestContext(request))
photo.html
{% for p in photos %}
<img src="{{ p.img.url }}"/>
{% endfor %}
urls.py
urlpatterns = patterns('',
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^photo/$', 'galery.views.photo'),
url(r'^site_media/(.*)$', 'django.views.static.serve', {'document_root' : os.path.join(os.path.dirname(__file__), 'site_media')}),
)
settings.py:
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
それを修正する方法は?