我正在使用CS50 IDE用Flask开发一个Web应用程序。我希望用户上传带有表单的文件。但是,提交表单后,无论是否未选择文件,我都会收到“ 500 Internal Server Error ”。
如果选择了文件,则控制台中没有错误日志。
如果选择否文件,则存在以下错误日志:
import psutil
import time
ini = 'start'
def getTaskList():
list_of_ran_proccesses = []
for procs in psutil.process_iter():
list_of_ran_proccesses.append(procs)
return list_of_ran_proccesses
def CompareRunningFiles():
if ini == "start":
list_of_old_procs = getTaskList()
while list_of_old_procs == getTaskList():
time.sleep(0.01)
for new_procs in psutil.process_iter():
print(new_procs.exe())
CompareRunningFiles()
烧瓶代码
ERROR:application:Exception on /new-item [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/final-project/helpers.py", line 34, in decorated_function
return f(*args, **kwargs)
File "/home/ubuntu/final-project/application.py", line 45, in decorated_function
return f(*args, **kwargs)
File "/home/ubuntu/final-project/application.py", line 350, in newItem
file.save(os.path.join(app.config['IMAGE_UPLOADS'], filename))
File "/usr/local/lib/python3.7/site-packages/werkzeug/datastructures.py", line 2800, in save
dst = open(dst, "wb")
IsADirectoryError: [Errno 21] Is a directory: 'static/img/menu/'
HTML代码
app.config["IMAGE_UPLOADS"] = "static/img/menu/"
@app.route("/new-item", methods=['GET', 'POST'])
def newItem():
if request.method == "POST":
if request.files:
file = request.files["file"]
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['IMAGE_UPLOADS'], filename))
您知道导致错误的原因是什么吗?
答案 0 :(得分:0)
您需要删除斜杠,这是对os.path.join
的误用
app.config["IMAGE_UPLOADS"] = "static/img/menu" # drop the trailing slash
请参见Why doesn't os.path.join() work in this case?
您可能还需要将app.config["IMAGE_UPLOADS"]
更新为绝对路径,而不是相对路径。