Category Archives: Django

Django snippet: reduce image size during upload

Just a quick python snippet I written today to reduce the image size during upload class PhotoField(forms.FileField, object): def __init__(self, *args, **kwargs): super(PhotoField, self).__init__(*args, **kwargs) self.help_text = “Images over 500kb will be resized to keep under 500kb limit, which may result in some loss of quality” def validate(self,image): if not str(image).split(‘.’)[-1].lower() in [“jpg”,”jpeg”,”png”,”gif”]: raise ValidationError(“File… Read More »

Creating custom log handler that logs to database models in django

I was told to create some kind of logging mechanism to insert logs into database, and make sure it’s generic. So after some thought I decided to mimic the builtin RotatingFileHandler and write a DBHandler. The RotatingFileHandler allows you to specify which file to write to and rotate files, therefore my DBHandler should also allow… Read More »