1

i have written a function inside a function for getting image height and width. but when i hit url all code works except get_image_size function this is my code

class ProductDetailView(DetailView):
    """
    Modified Context Data in PDP pages
    """
    def get_context_data(self, **kwargs):
        urlImage = 'http://'+ str(self.request.get_host()) + '/media/images/products/2016/12/dog.jpg'
        print "link is",urlImage


        def get_image_size(urlImage):
            print "<<<<<<<<<<<<<<<<<<<<<,,"
            data = requests.get(urlImage).content
            im = Image.open(BytesIO(data))    
            return im.size


        if __name__ == "__main__":
            print "===========>"
            width, height = get_image_size(urlImage)
            print "height is--", height
            print "width is --",width

what is the issue with it.

8
  • 1
    Probably that your function is local to get_context_data. You can't access it from outside. What's the traceback ? Are you getting any error ? Commented Dec 22, 2016 at 12:53
  • no i am not getting any error. just not getting this function working.outer def is working proporly. Commented Dec 22, 2016 at 12:54
  • Do you see any of your print ? Commented Dec 22, 2016 at 12:55
  • It should be data = self.requests.get(urlImage).content. Commented Dec 22, 2016 at 12:56
  • 1
    So the function was not your issue. Think more globally to find where the issue come from. If you don't see the print, that means that you don't even passed the main. Commented Dec 22, 2016 at 13:01

1 Answer 1

3

Django views is not executed as main program. If you print __name__ you'll see something like app_name.viewsand not __main__

Try to remove if __name__ == "__main__": validation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.