Passing csv contents to template. And getting error. thanks
def ....
with open('csv', 'rb') as f:
reader = csv.reader(f)
return render(request, template, {"results": reader})
Error
I/O operation on closed file
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
withintroduces a new context manager, which in the case of a file takes care of closing its argument after it ends. However, your context manager ends in the very next line, so by the time therenderfunction is called, the file is closed, and the csv reader can’t operate.You could probably just move the
returnone indentation to the right, or better call render within thewithbut save it to a variable which you subsequently pass toreturn: