0

模板

<span style="float: left; padding-top: 5px;"><a href="/media/{{image.path}}">{{image.name|slice:":25"}}</a></span>

设置.py

MEDIA_ROOT = '/tmp/'
MEDIA_URL = '/media/'

网址.py

 (r'^media/(?P<path>.*)$', 'incident.views.media_serve_protected'),

def media_serve_protected(request, path):
    if path.startswith("{id}/".format(id=request.user.id)):
        return serve(request, path, settings.MEDIA_ROOT)
    else:
        return HttpResponseForbidden()

我可以上传图像。上传的图像保存在 tmp 文件夹中。我可以看到带有图像 url 的图像名称,但是如果我单击图像 url 来查看,我会收到错误消息"Access to the webpage was denied You are not authorized to access the webpage at http://192.168.100.12/media/root/16/20130816235304-photo0015.jpg. You may need to sign in."

4

1 回答 1

2

如果urlmedia_serve_protected不以media/<id>. 但你的网址是形式media/root/<id>

于 2013-08-16T16:34:01.057 回答