Cloudinary Upload Issue on PythonAnywhere: MaxRetryError
I'm developing a Django application that uses Cloudinary for image storage. When running the project locally, everything works fine, and images upload successfully.
However, when I deploy the project on PythonAnywhere, I get the following error when trying to upload an image from the admin panel:
Unexpected error - MaxRetryError("TCPKeepAliveHTTPSConnectionPool(host='api.cloudinary.com', port=443): Max retries exceeded with url: /v1_1/di6geozk1/image/upload (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x748d7df0b520>: Failed to establish a new connection: [Errno 111] Connection refused'))")
Configuration in settings.py:
CLOUDINARY_STORAGE = {
'CLOUD_NAME': env('CLOUDINARY_STORAGE_CLOUD_NAME', default=''),
'API_KEY': env('CLOUDINARY_STORAGE_API_KEY', default=''),
'API_SECRET': env('CLOUDINARY_STORAGE_API_SECRET', default=''),
'SECURE': True,
'API_PROXY': 'http://proxy.server:3128'
}
### Things I've already checked:
- The credentials (CLOUD_NAME, API_KEY, API_SECRET) are correctly set.
- In my local environment, images upload successfully to Cloudinary.
- In PythonAnywhere, the connection is refused.
- I tried removing the 'API_PROXY' setting, but the error persists.
I found this on the Pythonanywhere forums: https://www.pythonanywhere.com/forums/topic/13776/#id_post_105423
This should resolve the issue
For security purposes, PythonAnywhere's free accounts' internet access goes via a proxy "allowlist"; they can only access sites that are on that list. Cloudinary is on that list (see the list of sites currently allowed.)
The [Errno 111] Connection refused error you're getting suggests that either Cloudinary doesn't support using a proxy, or that it needs to be configured to use a proxy. Have a look at their docs to determine which it is and consider raising an issue with the project about proxy support.
Also review your Cloudinary configuration to ensure that it includes the correct proxy settings. Do note that paying users don't have this limitation. See https://help.pythonanywhere.com/pages/403ForbiddenError/#proxy-details
NOTE: You can let PythonAnywhere serve media files for you. To do that:
in your settings.py set
MEDIA_ROOTto some path (like/home/yourusername/yourproject/media) andMEDIA_URLto/media/(or whatever URL you want)on a web app tab scroll down to
Static filessection and make sure there is a row with yourMEDIA_URLand the path you set forMEDIA_ROOT.restarting your web app and all user-uploaded content will be correctly served.