Categories
Python Answers

How to upload multiple files with Python Flask?

Sometimes, we want to upload multiple files with Python Flask.

In this article, we’ll look at how to upload multiple files with Python Flask.

How to upload multiple files with Python Flask?

To upload multiple files with Python Flask, we can use the request.files.getlist method.

For instance, we write

@app.route("/upload", methods=["POST"])
def upload():
    uploaded_files = flask.request.files.getlist("file[]")
    print(uploaded_files)
    return ""

to create the upload view that gets the files from flask.request.files.getlist with the key of the form data that has the files.

Then we can do whatever we want with the files in the uploaded_files list.

Conclusion

To upload multiple files with Python Flask, we can use the request.files.getlist method.

Categories
Python Answers

How to get raw POST body in Python Flask regardless of Content-Type header?

Sometimes, we want to get raw POST body in Python Flask regardless of Content-Type header.

In this article, we’ll look at how to get raw POST body in Python Flask regardless of Content-Type header.

How to get raw POST body in Python Flask regardless of Content-Type header?

To get raw POST body in Python Flask regardless of Content-Type header, we call request.stream.read.

For instance, we write

data = request.stream.read()

to get the raw data from the request that’s passed by the WSGI server to the Flask app and read it.

Conclusion

To get raw POST body in Python Flask regardless of Content-Type header, we call request.stream.read.

Categories
Python Answers

How to add optional URL parameters with Python Flask?

Sometimes, we want to add optional URL parameters with Python Flask

In this article, we’ll look at how to add optional URL parameters with Python Flask.

How to add optional URL parameters with Python Flask?

To add optional URL parameters with Python Flask, we can set the default value for URL parameters.

For instance, we write

@user.route('/<user_id>', defaults={'username': None})
@user.route('/<user_id>/<username>')
def show(user_id, username):
    pass

to add the routes that map to the show view.

We call the user.route decorator with the route patterns that have the username parameter and without.

And when we call route with a URL without the username parameter, we set username to None as the default value.

Conclusion

To add optional URL parameters with Python Flask, we can set the default value for URL parameters.

Categories
Python Answers

How to fix form sending error with Python Flask?

Sometimes, we want to fix form sending error with Python Flask

In this article, we’ll look at how to fix form sending error with Python Flask.

How to fix form sending error with Python Flask?

To fix form sending error with Python Flask, we use the request.form dict to get the form values with keys that match one of the name attribute of an input element.

For instance, we write

<input name="question_field" placeholder="question one">

to add an input with name set to question_field.

Then we write

request.form['question_field']

in our view function to get the value of the input with name attribute set to question_field.

Conclusion

To fix form sending error with Python Flask, we use the request.form dict to get the form values with keys that match one of the name attribute of an input element.

Categories
Python Answers

How to fix Python Flask app not picking up .css file?

Sometimes, we want to fix Python Flask app not picking up .css file.

In this article, we’ll look at how to fix Python Flask app not picking up .css file.

How to fix Python Flask app not picking up .css file?

To fix Python Flask app not picking up .css file, we should set up the static file folder for our Flask app.

We put the CSS file in the /static/styles folder in our Flask project folder.

Then we add the link tag for the CSS file by writing

<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}">

We set the href attribute to {{ url_for('static',filename='styles/mainpage.css') }}.

to reference the mainpage.css file with the link tag.

Conclusion

To fix Python Flask app not picking up .css file, we should set up the static file folder for our Flask app.

We put the CSS file in the /static/styles folder in our Flask project folder.