我不确定 r 应该做什么.没有它,它应该可以正常工作: fileanme = open(path_to_file,"rb") Here are the lines to read file & download in django :def download(request): file_name =request.GET.get('file_name', '') path_to_file = "C:\Users\CV_Uploads\uploadfiles\uploadfiles" + "\\" + file_name fileanme = open('r'. path_to_file , "rb") # **ERROR HERE** mimetype = mimetypes.guess_type('path_to_file')[0] if not mimetype: mimetype = "application/octet-stream" response = HttpResponse(fileanme.read(), mimetype=mimetype) response["Content-Disposition"]= "attachment; filename=%s" % os.path.split(path_to_file)[1] return responseProblem:Was working fine with hardcoded values but now as i make this code dynamic by joining path_to_file with file_name . It says: AttributeError at /download/ . . . .'str' object has no attribute 'path_to_file'How to solve this attribute error? 解决方案 Well, that code is clearly wrong. As the error says, you're taking the string "r", and using the dot notation to call a path_to_file attribute on it, which doesn't exist.I'm not sure what that r is supposed to be doing at all. It should work fine without it:fileanme = open(path_to_file , "rb") 这篇关于django中的文件读取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 17:33