Pythonを使用しているユーザーが提供するファイルを読む/ダウンロードする必要がありますが、構文エラーです。Pythonを使用してファイルをダウンロードしようとしているときに構文エラーが発生する
エラー:
File "path1.py", line 6 return HttpResponse(content=test_file, content_type="text/plain") SyntaxError: 'return' outside function
私は以下の私のコードを説明しています。
import os
from django.http import HttpResponse
#image_name = request.GET.get('param')
image_name = "63b6ac1bc61642f39c697585810aed17.txt"
test_file = os.path.join(os.getcwd(), image_name)
return HttpResponse(content=test_file, content_type="text/plain")
実際には、私がファイル名を提供していて、このファイルをPythonを使用して読み込み/ダウンロードする必要があるのです。
return文を削除します。
はこれを試してみてください。関数内でのみ返ることができます。 –