如何在模板中显示图像?
我尝试这样的事情(但不起作用):
这是我的模型:
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
视图.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))
照片.html
{% for p in photos %}
<img src="{{ p.img.url }}"/>
{% endfor %}
网址.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')}),
)
设置.py:
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
如何解决?