Django拒绝访问该网页

时间:2013-08-16 16:21:22

标签: python django django-models django-templates django-admin

模板

<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."

1 个答案:

答案 0 :(得分:2)

如果网址不以media_serve_protected开头,则您的media/<id>函数会返回Forbidden响应。但您的网址格式为media/root/<id>