By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,421 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,421 IT Pros & Developers. It's quick & easy.

Django: Indentation Error: Unexpected indent

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

Expand|Select|Wrap|Line Numbers
  1. from django.db.models.fields.files import ImageField, ImageFieldFile
  2. from PIL import Image
  3. import os
  4.  
  5. def _add_thumb(s):
  6.     """
  7.     Modifies a string (filename, URL) containing an image filename, to insert
  8.     '.thumb' before the file extension (which is changed to be '.jpg').
  9.     """
  10.     parts = s.split(".")
  11.     parts.insert(-1, "thumb")
  12.     if parts[-1].lower() not in ['jpeg', 'jpg']:
  13.         parts[-1] = 'jpg'
  14.     return ".".join(parts)
  15.  
  16. class ThumbnailImageField(ImageField):
  17.     """
  18.     Behaves like a regular ImageField, but stores an extra (JPEG) thumbnail
  19.     image, providing get_FIELD_thumb_url() and get_FIELD_thumb_filename().
  20.  
  21.     Accepts two additional, optional arguments: thumb_width and thumb_height,
  22.     both defaulting to 128 (pixels). Resizing will preserve aspect ratio while
  23.     staying inside the requested dimensions; see PIL's Image.thumbnail()
  24.     method documentation for details.
  25.     """
  26.     attr_class = ThumbnailImageFieldFile
  27.  
  28.  
  29.  
  30.     def __init__(self, thumb_width=128, thumb_height=128, *args, **kwargs):
  31.         self.thumb_width = thumb_width
  32.         self.thumb_height = thumb_height
  33.         super(ThumbnailImageField, self).__init__(*args, **kwargs)
  34.  
  35.     def _get_path(self):
  36.         self._require_file()
  37.                   return self.storage.path(self.name)
  38.                   #^Error!
  39.     path = property(_get_path)
  40.  
  41.     class ThumbnailImageFieldFile(ImageFieldFile):
  42.         def _get_thumb_path(self):
  43.             return _add_thumb(self.path)
  44.         thumb_path = property(_get_thumb_path)
  45.  
  46.         def _get_thumb_url(self):
  47.             return _add_thumb(self.url)
  48.         thumb_url = property(_get_thumb_url)
  49.  
  50.         def save(self, name, content, save=True):
  51.             super(ThumbnailImageFieldFile, self).save(name, content, save)
  52.             img = Image.open(self.path)
  53.             img.thumbnail(
  54.                 (self.field.thumb_width, self.field.thumb_height),
  55.                 Image.ANTIALIAS
  56.             )
  57.             img.save(self.thumb_path, 'JPEG')
  58.  
  59.         def delete(self, save=True):
  60.             if os.path.exists(self.thumb_path):
  61.                 os.remove(self.thumb_path)
  62.             super(ThumbnailImageFieldFile, self).delete(save)
  63.  
Thanks if you can help me.
Mar 25 '12 #1

✓ answered by bvdet

It should look like this:
Expand|Select|Wrap|Line Numbers
  1.     def _get_path(self):
  2.         self._require_file()
  3.         return self.storage.path(self.name)
  4.  
Indentation is critical to Python. Perhaps you should study the Python documentation to learn indentation rules.

Share this Question
Share on Google+
4 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
It should look like this:
Expand|Select|Wrap|Line Numbers
  1.     def _get_path(self):
  2.         self._require_file()
  3.         return self.storage.path(self.name)
  4.  
Indentation is critical to Python. Perhaps you should study the Python documentation to learn indentation rules.
Mar 25 '12 #2

P: 11
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 "ThumbnailImageFieldFile is not defined". Could you please help me again?
Mar 25 '12 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Move line attr_class = ThumbnailImageFieldFile to the last line of the script. The assignment was attempted before ThumbnailImageFieldFile was defined.
Mar 25 '12 #4

P: 11
Okay. Thanks for your help but my code still contains other error.. As a beginner I am very sad
Mar 30 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.