我有一个问题,我必须安全地让登录的用户从指定的路径访问本地目录内容。/djangapp/media/user1也就是说,当user1登录时,他们应该只能从/djangapp/media/user1访问内容
我目前的看法是:
def get_absolute_pathname(pathname='', safe=True):
if not pathname:
return os.path.join(MEDIA_ROOT, 'index')
if safe and '..' in pathname.split(os.path.sep):
return get_absolute_pathname(pathname='')
return os.path.join(MEDIA_ROOT, pathname)
@login_required
def retrieve_path(request, document_root, pathname=''):
pathname = None
if request.user.is_authenticated():
pathname = request.user.get_username()
abs_pathname = get_absolute_pathname(pathname)
url = document_root
response = HttpResponseRedirect(url)
return response
当前URL为:
^{pr2}$我可以直接从url访问http://127.0.0.1:8000/DjangoApp/static/。但我想限制访问。在
我做错了什么?如何使访问通过身份验证并且仅限于固定路径?在
谢谢你
安全的媒体文件不被匿名用户服务更好的网址保护。。。在
使用@
login_required
和def protected_serve(request, path, document_root=None):
可以保护它。。在更多信息
How to to make a file private by securing the url that only authenticated users can see
相关问题 更多 >
编程相关推荐