Why did Flask start failing with “ImportError: cannot import name ‘url_quote’ from ‘werkzeug.urls'”?

I had the same problem. It is because Werkzeug 3.0.0 was released and Flask doesn’t specify the dependency correctly (requirements says Werkzeug>=2.2.0). This is why, Werkzeug 3.0.0 is still installed and Flask 2.2.2 isn’t made for Werkzeug 3.0.0. Solution: Just set a fix version for Werkzeug such as Werkzeug==2.2.2 in your requirements.txt and it should … Read more

X-Forwarded-Proto and Flask

You are missing the ProxyFix() middleware component. See the Flask Proxy Setups documentation. There is no need to subclass anything; simply add this middleware component to your WSGI stack: # Werkzeug 0.15 and newer from werkzeug.middleware.proxy_fix import ProxyFix from flask import Flask app = Flask(__name__) app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1) If you have Flask installed, you … Read more

Custom error message json object with flask-restful

People tend to overuse abort(), while in fact it is very simple to generate your own errors. You can write a function that generates custom errors easily, here is one that matches your JSON: def make_error(status_code, sub_code, message, action): response = jsonify({ ‘status’: status_code, ‘sub_code’: sub_code, ‘message’: message, ‘action’: action }) response.status_code = status_code return … Read more

How to apply integration tests to a Flask RESTful API

Flask provides a test_client you can use in your tests: from source.api import app from unittest import TestCase class TestIntegrations(TestCase): def setUp(self): self.app = app.test_client() def test_thing(self): response = self.app.get(“https://stackoverflow.com/”) assert <make your assertion here> Flask Testing Docs

AttributeError: ‘Context’ object has no attribute ‘wrap_socket’

As of 0.10, Werkzeug doesn’t support OpenSSL contexts anymore. This decision was made because it is easier to support ssl.SSLContext across Python versions. Your option to re-write this code is this one: if __name__ == “__main__”: context = (‘cert.crt’, ‘key.key’) app.run(host=”0.0.0.0″, port=80, ssl_context=context, threaded=True, debug=True) See http://werkzeug.pocoo.org/docs/latest/serving/ for all possibilities.

ImportError: cannot import name ‘url_quote’ from ‘werkzeug.urls’

I had the same problem. It is because Werkzeug 3.0.0 was released and Flask doesn’t specify the dependency correctly (requirements says Werkzeug>=2.2.0). This is why, Werkzeug 3.0.0 is still installed and Flask 2.2.2 isn’t made for Werkzeug 3.0.0. Solution: Just set a fix version for Werkzeug such as Werkzeug==2.2.2 in your requirements.txt and it should … Read more

ImportError: cannot import name ‘safe_str_cmp’ from ‘werkzeug.security’

Werkzeug released v2.1.0 today, removing werkzeug.security.safe_str_cmp. You can probably resolve this issue by pinning Werkzeug~=2.0.0 in your requirements.txt file (or similar). pip install Werkzeug~=2.0.0 After that it is likely that you will also have an AttributeError related to the jinja package, so if you have it, also run: pip install jinja2~=3.0.3

error code: 521