How to upload image through Django admin page

Comments

6 comments

  • Avatar
    yiwei

    no need i already solved!! 

    0
    Comment actions Permalink
  • Avatar
    Aleksandar Kostadinov

    Hi,

    Thanks for your update, glad you were able to resolve it. I see that you are using the 3rd party library Django Cloudinary Storage. Would you be able to share your solution and what you have modified to make it work in case any other user comes across this post?

    0
    Comment actions Permalink
  • Avatar
    yiwei

    Hi Aleksandar,

    Thank you for your comment. Here are a few steps I did to address the issue:

    1. I traced back the error message and first check the source code of utils.py where defines the sign request function as below.

    Nothing was passed as params or options, so obviously cloudinary.config() method does not obtain the api_key.

    def sign_request(params, options):
        api_key = options.get("api_key", cloudinary.config().api_key)
        if not api_key:
            raise ValueError("Must supply api_key")
        api_secret = options.get("api_secret", cloudinary.config().api_secret)
        if not api_secret:
            raise ValueError("Must supply api_secret")

        params = cleanup_params(params)
        params["signature"] = api_sign_request(params, api_secret)
        params["api_key"] = api_key

        return params

    2. Next I trace back to the config() method in cloudinary.__init__.py file, where defines the Config class used to access the settings.py and grab environment API credentials. 

    class Config(BaseConfig):
        def __init__(self):
            self._uri_scheme = URI_SCHEME
            django_settings = import_django_settings()

            if django_settings:
                 self.update(**django_settings)

    ...

    3. Here the Django settings are imported via import_django_settings function, inside this function, this is how it grabs the settings:

    from django.conf import settings as _django_settings

    if 'CLOUDINARY' in dir(_django_settings):
        return _django_settings.CLOUDINARY
    else:
        return None

    4. Then I run the shell in Django and test if I got a 'None' by running this code below, which returns false.

    from django.conf import settings

    'CLOUDINARY' in dir(settings)

    5. So the reason is still in my settings, then I try to add config method directly in settings.py file:

    cloudinary.config(
    cloud_name="name",
    api_key="key",
    api_secret="secret"
    )

    Now when I upload again it works and no more error. The interesting thing is that when I run step 4 I got a false, I added these settings below but it didn't work, still got a false, not sure why. 

    CLOUDINARY = {
    'CLOUD_NAME': 'name',
    'API_KEY': 'key',
    'API_SECRET': 'secret'
    }

    In conclusion, I misunderstood that the Cloudinary storage setting and Cloudinary setting are interchangeable/same, but by checking the source code of how Cloudinary grabs those settings, I realize they are different and need to set the same credentials twice for both of them. A little bit confusing since I thought the Django Cloudinary Storage is a kind of 'official' package.

    0
    Comment actions Permalink
  • Avatar
    MIchael

    Please how were you able to implement that feature? I mean to be able to upload via the admin page.

    I'm experiencing issues trying to do that. I keep getting an Invalid Signature error.

    0
    Comment actions Permalink
  • Avatar
    Ranson

    Hey Michael,

     

    The Invalid Signature error is thrown when Signed uploads are enabled on an upload preset that is enabled for the upload. 

    Please see our documentation below for additional information on signed/unsigned uploads.

    https://cloudinary.com/documentation/upload_images#uploading_assets_to_the_cloud

    https://cloudinary.com/documentation/upload_presets

     

    This can be disabled for the upload method you are using, or alternatively, you can continue utilizing the signed uploads and provide a signature with the request being made. 

    0
    Comment actions Permalink
  • Avatar
    MIchael

    I have read it. I added my cloudinary configurations in my settings.py file and still have the same issue.

    0
    Comment actions Permalink

Please sign in to leave a comment.