login_required
デコレータを使用すると奇妙な問題が発生します。デコレータを使用して、ファイルを提供するビューへのアクセスをログイン ユーザーに制限しています。
Safari でテストすると、すべてが期待どおりに機能し、ログインしているユーザーはファイルをダウンロードできますが、ログインしていないユーザーはダウンロードできません。一方、Chrome と Firefox でのテストでは偽陰性となり、ログイン リダイレクトが発生します。リダイレクトはlogin_required
関数内で既に発生しており、view
入力されていません。
私は同じユーザーでテストしています。ユーザーは両方ともログインしています。興味深いことに、この唯一のファイル ダウンロード ビューでは偽陰性しか得られません。
で Django を実行していDebug=True
ます。
要求された URL は次のとおりです。
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
それはとても奇妙な問題なので、私が見ることができるヒントをいただければ幸いです。