我在 Django 模板中显示图像时遇到问题(我正在从管理应用程序上传图像)。我阅读了有关的文档和其他帖子,但upload_to
仍然无法弄清楚。我在我的模板中尝试了这个<img src="{{ a.image}}"/>
,然后得到了<img src="{{MEDIA_URL}}{{ a.image}}"/>
同样的结果。这是我的 settings.py 代码:
MEDIA_ROOT = '/home/mohamed/code/eclipse workspace/skempi0/media'
MEDIA_URL = '/media/'
最后,我在我的 models.py 中尝试了以下内容,但我失败了:
image = models.ImageField(upload_to = "ads/")
image = models.ImageField(upload_to = ".")
当我使用时image = models.ImageField(upload_to = MEDIA_URL)
出现以下错误
SuspiciousOperation at /admin/advertisments/banner/add/
Attempted access to '/media/cut.jpg' denied.
编辑
生成的链接如下:
<img src="./DSCN6671.JPG">
重新编辑
这是我的看法:
def index(request):
spotlightAlbum = Album.objects.filter(spotlight = True)
spotlightSong = Song.objects.filter(spotlight = True).order_by('numberOfPlays')
homepage = Song.objects.filter(homepage = True).order_by('numberOfPlays')
ads = Banner.objects.all()
copyright = CopyrightPage.objects.get()
try:
user = User.objects.get(userSlug = "mohamed-turki")
playlists = UserPlaylist.objects.filter(owner = user.userFacebookId)
purchase = Purchase.objects.filter(userName = user.userFacebookId)
user.loginState = 1
user.save()
except:
user = None
playlists = None
context = {'copyright':copyright, 'ads':ads, 'spotlightSong':spotlightSong,'spotlightAlbum': spotlightAlbum, 'homepage':homepage, 'user':user, 'playlists':playlists, 'purchase':purchase }
return render_to_response('index.html',context,context_instance = RequestContext(request))
谁能告诉我我做错了什么?PS我正在使用Django 1.4