Django: Indentation Error: Unexpected indent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imhomer
    New Member
    • Feb 2012
    • 11

    Django: Indentation Error: Unexpected indent

    The tutorial taught me to build a new file field. However, when I run the server, Indentation Error happened on line 37:

    Code:
    from django.db.models.fields.files import ImageField, ImageFieldFile
    from PIL import Image
    import os
    
    def _add_thumb(s):
        """
        Modifies a string (filename, URL) containing an image filename, to insert
        '.thumb' before the file extension (which is changed to be '.jpg').
        """
        parts = s.split(".")
        parts.insert(-1, "thumb")
        if parts[-1].lower() not in ['jpeg', 'jpg']:
            parts[-1] = 'jpg'
        return ".".join(parts)
    
    class ThumbnailImageField(ImageField):
        """
        Behaves like a regular ImageField, but stores an extra (JPEG) thumbnail
        image, providing get_FIELD_thumb_url() and get_FIELD_thumb_filename().
    
        Accepts two additional, optional arguments: thumb_width and thumb_height,
        both defaulting to 128 (pixels). Resizing will preserve aspect ratio while
        staying inside the requested dimensions; see PIL's Image.thumbnail()
        method documentation for details.
        """
        attr_class = ThumbnailImageFieldFile
    
    					  
    
        def __init__(self, thumb_width=128, thumb_height=128, *args, **kwargs):
            self.thumb_width = thumb_width
            self.thumb_height = thumb_height
            super(ThumbnailImageField, self).__init__(*args, **kwargs)
    
        def _get_path(self):
            self._require_file()
                      return self.storage.path(self.name)
                      #^Error!
        path = property(_get_path)
    
        class ThumbnailImageFieldFile(ImageFieldFile):
            def _get_thumb_path(self):
                return _add_thumb(self.path)
            thumb_path = property(_get_thumb_path)
    
            def _get_thumb_url(self):
                return _add_thumb(self.url)
            thumb_url = property(_get_thumb_url)
    
            def save(self, name, content, save=True):
                super(ThumbnailImageFieldFile, self).save(name, content, save)
                img = Image.open(self.path)
                img.thumbnail(
                    (self.field.thumb_width, self.field.thumb_height),
                    Image.ANTIALIAS
                )
                img.save(self.thumb_path, 'JPEG')
    
            def delete(self, save=True):
                if os.path.exists(self.thumb_path):
                    os.remove(self.thumb_path)
                super(ThumbnailImageFieldFile, self).delete(save)
    Thanks if you can help me.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    It should look like this:
    Code:
        def _get_path(self):
            self._require_file()
            return self.storage.path(self.name)
    Indentation is critical to Python. Perhaps you should study the Python documentation to learn indentation rules.

    Comment

    • imhomer
      New Member
      • Feb 2012
      • 11

      #3
      Oh.. It is a stupid error.. Thanks for your help. By the way, I tried to runserver again, and a name error happened in line 26, which says "ThumbnailImage FieldFile is not defined". Could you please help me again?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Move line attr_class = ThumbnailImageF ieldFile to the last line of the script. The assignment was attempted before ThumbnailImageF ieldFile was defined.

        Comment

        • imhomer
          New Member
          • Feb 2012
          • 11

          #5
          Okay. Thanks for your help but my code still contains other error.. As a beginner I am very sad

          Comment

          Working...