I'm a student of engineering and I'm supposed to create a little app for a project using python-flask, but at the moment I'm stuck at the construction of the Upload system: I've just copied the code provided by the professor but it doesn't work, it says "Errno30, read-only file system /static". I couldn't find a way to solve it so need help.
Pasting here the view function:
@app.route("/upload", methods=["POST", "GET"])
def upload_page():
folder_name = str(session.get('park'))
park=Parks.query.filter_by(id_park=folder_name).first()
if not os.path.exists('/static/Uploads/' + str(folder_name)):
os.makedirs('/static/Uploads/' + str(folder_name))
file_url = os.listdir('/static/Uploads/' + str(folder_name))
file_url = [str(folder_name) + "/" + file for file in file_url]
formupload = UploadForm()
print folder_name
if formupload.validate_on_submit():
filename = photos.save(formupload.file.data,
name=folder_name + '.jpg', folder=folder_name)
file_url.append(filename)
park.image = file_url
db.session.commit()
return redirect(url_for("home_page"))
return render_template("upload.html", formupload=formupload, filelist=file_url)
I'm not sure at all of whatever is written here, but I have to say that "Uploads" is a folder that I've created just now for this, and it is inside the folder "static".
I paste even the error page:
OSError
OSError: [Errno 30] Read-only file system: '/static'
Traceback (most recent call last)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/riccardo/PycharmProjects/flaskProject/app.py", line 130, in upload_page
os.makedirs('/static/Uploads/' + str(folder_name))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 150, in makedirs
makedirs(head, mode)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 150, in makedirs
makedirs(head, mode)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 30] Read-only file system: '/static'
The error message is pretty self-explaining.
Your code tries to write to
/static/Uploads
which you are not allowed to write to. Probably for a good reason, as/static/Uploads
is an absolute URL and very high up in the file system.You probably want to write into either the app's directory or into another folder in the app's user directory.
You could remove the leading slash of the path to make it a relative URL.
Also please note that
Flask-Uploads
is obsolete - you should use https://github.com/jugmac00/flask-reuploadedAlso, you use Python 2.7, which is no longer maintained. You should use Python 3.10.