我使用login_required
装饰器有一个奇怪的问题。我正在使用装饰器来限制访问为登录用户提供文件的视图。
使用Safari进行测试时,一切都按预期工作,登录用户可以下载文件 - 未登录的用户不能。而使用Chrome和Firefox进行测试会产生误报,导致登录重定向。重定向已在login_required
函数中发生,未输入view
。
我正在使用同一个用户进行测试,用户已登录两次,有趣的是我只能通过这个文件下载视图获得漏报。
在Debug=True
上运行Django。
请求的网址是:
http://localhost:8000/media/accounting/invoices/First-Budget-PO1-AR-2014-007.pdf
导致重定向:
http://localhost:8000/?next=/media/accounting/invoices/First-Budget-PO1-AR-2014-007.pdf
urls.py
url(r'^media/(?P<file_path>.*)$', login_required(FileDownloadHandler.as_view()), name='file_download_handler' ),
views.py(而我认为它没有影响,因为没有输入)
class FileDownloadHandler(View):
"""
download handler for user uploaded files
"""
def get(self, request, file_path, **kwargs):
mimetypes.init()
#left out details - sanity checks etc for simplicity reasons
#no redirect happens here.. chrome request do not even enter the view
fsock = open(file_path,"r")
file_name = os.path.basename(file_path)
mime_type_guess = mimetypes.guess_type(file_name)
if mime_type_guess is not None:
response = HttpResponse(fsock, mimetype=mime_type_guess[0])
response['Content-Disposition'] = 'attachment; filename=' + file_name
else:
response = HttpResponseNotFound()
return response
我会欣赏任何可以看到的提示,因为这是一个非常奇怪的问题。