UnicodeEncodeError: ‘ascii’ codec can’t encode character

For anyone encountering this problem when running Django with Supervisor, the solution is to add e.g. the following to the supervisord section of Supervisor’s configuration: environment=LANG=”en_US.utf8″, LC_ALL=”en_US.UTF-8″, LC_LANG=”en_US.UTF-8″ This solved the problem for me in Supervisor 3.0a8 running on Debian Squeeze. Also make sure Supervisor re-reads the configuration by running: supervisorctl reread supervisorctl restart myservice … Read more

Django: Validate file type of uploaded file

Validating files is a common challenge, so I would like to use a validator: import magic from django.utils.deconstruct import deconstructible from django.template.defaultfilters import filesizeformat @deconstructible class FileValidator(object): error_messages = { ‘max_size’: (“Ensure this file size is not greater than %(max_size)s.” ” Your file size is %(size)s.”), ‘min_size’: (“Ensure this file size is not less than … Read more

How do you convert a PIL `Image` to a Django `File`?

The way to do this without having to write back to the filesystem, and then bring the file back into memory via an open call, is to make use of StringIO and Django InMemoryUploadedFile. Here is a quick sample on how you might do this. This assumes that you already have a thumbnailed image named … Read more

tech