模板
<span style="float: left; padding-top: 5px;"><a href="/media/{{image.path}}">{{image.name|slice:":25"}}</a></span>
settings.py
MEDIA_ROOT = '/tmp/'
MEDIA_URL = '/media/'
urls.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文件夹中。我可以看到带图片网址的图片名称,但查看是否点击图片网址我收到错误"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."
答案 0 :(得分:2)
如果网址不以media_serve_protected
开头,则您的media/<id>
函数会返回Forbidden响应。但您的网址格式为media/root/<id>
。