Finally getting back into the swing of things in studying django. I discovered 'Werkzeug debugger' and found out I was doing many things wrong (saving to wrong place, trying to open the Image prior to saving it locally, etc.).
Great debugging tool (wish I had an IDE, but this is better than printing to files for debugging info, etc.). Any other great debugging tool you guys have used? On Thu, May 14, 2009 at 11:04 PM, MrBodjangos <mrangelc...@gmail.com> wrote: > Hi, > > Please give me pointers on how to debug this. > > Basic form error: > > "Upload a valid image. The file you uploaded was either not an image > or a corrupted image." > > I have googled around and the problems I have read regards recompiling > jpeg for MAC after installing PIL. I have Windows, and so I believe > everything is bundled correctly. > > I have a Windows XP box, Python 2.5, django 1.0, and Python Imaging > Library 1.1.6 for Python 2.5 (Windows only). > > I have created a simple form with a browse and submit. > > I upload a jpg file and the form keeps returning "Upload a valid > image. The file you uploaded was either not an image or a corrupted > image." > > Now, I know PIL is working because in the shell, I can do this: > -------------------------------------------------------------- > $ python manage.py shell > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit > (Intel)] on > win32 > Type "help", "copyright", "credits" or "license" for more information. > (InteractiveConsole) > >>> from PIL import Image > >>> im=Image.open("C:\\my_wsgi\\media\\photos\\name.jpg") > >>> print im.format > JPEG > >>> > -------------------------------------------------------------- > > In my google search efforts, one suggestion was to just change the > raise exception in fields.py (C:\Python25\Lib\site-packages\django > \forms): > > class ImageField(FileField): > . > . > . > . > try: > # load() is the only method that can spot a truncated > JPEG, > # but it cannot be called sanely after verify() > trial_image = Image.open(file) > trial_image.load() > > # Since we're about to use the file again we have to reset > the > # file object if possible. > if hasattr(file, 'reset'): > file.reset() > > # verify() is the only method that can spot a corrupt PNG, > # but it must be called immediately after the constructor > trial_image = Image.open(file) > trial_image.verify() > except ImportError: > # Under PyPy, it is possible to import PIL. However, the > underlying > # _imaging C module isn't available, so an ImportError > will be > # raised. Catch and re-raise. > raise > except Exception: # Python Imaging Library doesn't recognize > it as an image > > raise # << I comment the original below, and I just call > raise here >>> > #raise ValidationError(self.error_messages > ['invalid_image']) > . > . > . > > > Having the raise bubble all the way up leads to: > > TemplateSyntaxError at /upload_file/ > > Caught an exception while rendering: cannot identify image file > > Original Traceback (most recent call last): > File "C:\Python25\lib\site-packages\django\template\debug.py", line > 71, in render_node > result = node.render(context) > File "C:\Python25\lib\site-packages\django\template\defaulttags.py", > line 241, in render > value = bool_expr.resolve(context, True) > File "C:\Python25\lib\site-packages\django\template\__init__.py", > line 535, in resolve > obj = self.var.resolve(context) > File "C:\Python25\lib\site-packages\django\template\__init__.py", > line 676, in resolve > value = self._resolve_lookup(context) > File "C:\Python25\lib\site-packages\django\template\__init__.py", > line 705, in _resolve_lookup > current = getattr(current, bit) > File "C:\Python25\lib\site-packages\django\forms\forms.py", line > 340, in _errors > return self.form.errors.get(self.name, self.form.error_class()) > File "C:\Python25\lib\site-packages\django\forms\forms.py", line > 111, in _get_errors > self.full_clean() > File "C:\Python25\lib\site-packages\django\forms\forms.py", line > 229, in full_clean > value = field.clean(value, initial) > File "c:\Python25\lib\site-packages\django\forms\fields.py", line > 505, in clean > trial_image = Image.open(file) > File "C:\Python25\Lib\site-packages\PIL\Image.py", line 1916, in > open > raise IOError("cannot identify image file") > IOError: cannot identify image file > > -------------------------------------------------------------------- > > So, it seems that one of these two are failing in fields.py: > trial_image = Image.open(file) > trial_image.load() > > Here is my question: > How do I find out what that 'file' is the Image object is trying to > open? > It would be nice if I can just put a print statement in between the > two, something like: > > trial_image = Image.open(file) > print "here is the file you are loading: "+str(file) > trial_image.load() > > But of course, that will not work. > I think that 'file' is pointing in the wrong place and hence, PIL > cannot find it. > > P.S. > To make the debugging easier, I hardcoded the filename in my file > handler to ensure it gets copied in the right place: > > def handle_uploaded_file(f): > out_file="C:\\my_wsgi\\media\\photos\\name.jpg" > destination = open(out_file, 'wb+') > for chunk in f.chunks(): > destination.write(chunk) > destination.close() > > And I do see the file in c:\my_wsgi\media\photos directory, and so I > know that chunks are writtern successfully. > > So, please, what's a good debugging method for me to capture to what > that 'file' is pointing? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---