Categories
Python Answers

How to redirect to https from http with Python Flask?

Sometimes, we want to redirect to https from http with Python Flask.

In this article, we’ll look at how to redirect to https from http with Python Flask.

How to redirect to https from http with Python Flask?

To redirect to https from http with Python Flask, we redirect from the http URL to the https URL with request.url.replace.

For instance, we write

@app.before_request
def before_request():
    if not request.is_secure:
        url = request.url.replace("http://", "https://", 1)
        code = 301
        return redirect(url, code=code)

to apply the @app.before_request decorator to the before_request function to call it before each request.

In it, we check if a https request is made with request.is_secure.

If it False, then we call request.url.replace to replace 'http://' with 'https://' in the URL.

We call redirect with the new url with 'https://' and we set the response code to 301 to do a 301 redirect to the https URL from the http URL.

Conclusion

To redirect to https from http with Python Flask, we redirect from the http URL to the https URL with request.url.replace.

Categories
Python Answers

How to set content type with Python Flask?

Sometimes, we want to set content type with Python Flask.

In this article, we’ll look at how to set content type with Python Flask.

How to set content type with Python Flask?

To set content type with Python Flask, we can use the Response class.

For instance, we write

from flask import Response


@app.route("/ajax_ddl")
def ajax_ddl():
    xml = "foo"
    return Response(xml, mimetype="text/xml")

to create a Response object with the xml string as the body and the mimetype set to the MIME type of the response.

Conclusion

To set content type with Python Flask, we can use the Response class.

Categories
Python Answers

How to fix ImportError: No module named MySQLdb with Python?

Sometimes, we want to fix ImportError: No module named MySQLdb with Python.

In this article, we’ll look at how to fix ImportError: No module named MySQLdb with Python.

How to fix ImportError: No module named MySQLdb with Python?

To fix ImportError: No module named MySQLdb with Python, we install the pymysql package.

To install it, we run

pip install pymysql

Then we add a connection string with

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://.....'

Conclusion

To fix ImportError: No module named MySQLdb with Python, we install the pymysql package.

Categories
Python Answers

How to pass arguments into redirect(url_for()) of Flask?

Sometimes, we want to pass arguments into redirect(url_for()) of Flask.

In this article, we’ll look at how to pass arguments into redirect(url_for()) of Flask.

How to pass arguments into redirect(url_for()) of Flask?

To pass arguments into redirect(url_for()) of Flask, we define the destination route to get the request parameters.

Then we can call url_for with the parameters.

For instance, we write

@app.route("/found/<email>/<list_of_objects>")
def found(email, list_of_objects):
    return render_template("found.html", keys=email, obj=list_of_objects)

to add the /found/<email>/<list_of_objects> route that maps to the found function.

In it, we get the the URL parameters from the found function’s parameters.

Then in another route, we write

return redirect(url_for("found", email=x, list_of_objects=y))

to call url_for with the route name and the parameters to return the URL for the /found/<email>/<list_of_objects> route with the parameters filled in.

Then we call redirect with the URL to redirect to the /found/<email>/<list_of_objects> route.

Conclusion

To pass arguments into redirect(url_for()) of Flask, we define the destination route to get the request parameters.

Categories
Python Answers

How to debug a Python Flask app running in Gunicorn?

Sometimes, we want to debug a Python Flask app running in Gunicorn.

In this article, we’ll look at how to debug a Python Flask app running in Gunicorn.

How to debug a Python Flask app running in Gunicorn?

To debug a Python Flask app running in Gunicorn, we can set the DEBUG setting to True.

For instance, we write

app = Flask(__name__)
app.config.from_pyfile('config.py')

to read the config from config.py with from_pyfile.

Then we write

DEBUG = True

to set the DEBUG setting to True to enable debugging in our Flask app.

Conclusion

To debug a Python Flask app running in Gunicorn, we can set the DEBUG setting to True.