0

I have some problem running uwsgi under supervisord.

The main problem is that with command line, I run uwsgi --ini /home/satori/mysite/src/mysite_uwsgi.ini, everythings goes well, but when I run it under supervisorctl, the server returned 500 when I upload a file with non-ascii character name.

But it won't happen if I run in the command line, the file is correctly uploaded.

I think the problen is related to my supervisor config file, maybe some environment issues. My config file is in /etc/supervisord.conf

This is my mysite_uwsgi.ini file, it's in /home/satori/mysite/src/mysite_uwsgi.ini

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /home/satori/mysite/src
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
#home            = /home/ubuntu/mysite/bin

#plugins         = python34

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /home/satori/mysite/src/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

and this is the related settings in supervisord.conf

[program:mysite]
command=uwsgi --ini /home/satori/mysite/src/mysite_uwsgi.ini
autostart=true
autorestart=true
user=root
log_stderr=true
stdout_logfile=/var/log/mysite.log

And this is the 500 error:

UnicodeEncodeError at /upload/view/
'ascii' codec can't encode characters in position 37-38: ordinal not in range(128)

models.py

# encoding: utf-8
import os
from django.db import models
from django.conf import settings


def get_upload_path(instance, filename):
    filename = ''.join([ch for ch in filename if ord(ch) < 128])
    return os.path.join("user_%d" % instance.owner.id, filename)


class Picture(models.Model):
    """This is a small demo using just two fields. The slug field is really not
    necessary, but makes the code simpler. ImageField depends on PIL or
    pillow (where Pillow is easily installable in a virtualenv. If you have
    problems installing pillow, use a more generic FileField instead.
    """
    file = models.ImageField(upload_to=get_upload_path)
    slug = models.SlugField(max_length=50, blank=True)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

    def __str__(self):
        return self.file.name

    @models.permalink
    def get_absolute_url(self):
        return ('upload-new', )

    def save(self, *args, **kwargs):
        self.slug = self.file.name
        super(Picture, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        """delete -- Remove to leave file."""
        self.file.delete(False)
        super(Picture, self).delete(*args, **kwargs)

1 Answers1

0

You should add this to your uwsgi.ini file

env = PYTHONIOENCODING=UTF-8

Also, not sure if this is what you did, but I used str instead of unicode. I was using a plugin which required str, in that case you can write:

def __str__(self):
    return YourModel.__unicode__(self)

Joris Kok
  • 113
  • 12
  • Thanks for your reply, but it doesn't work though. I turned to remove the non ascii characters in file name instead, it works OK. As I know, encoding is always a big problem. – Satori Komeiji Jul 17 '17 at 11:35
  • And I added my models to the question, if you have any ideas what's going on, please let me know. Thanks a lot. – Satori Komeiji Jul 17 '17 at 11:38
  • In your model there doesn't seem to be a __unicode__ method. Add that one and return self.file.name. Then add a __str__ method in case you need it, looking like my edited post. That worked for me. It shouldn't be related to your uwsgi and supervisord. Although I think that during the development it's better to set processes to 1. – Joris Kok Jul 17 '17 at 14:44
  • 2
    Thank you bro, but it doesn't work. I insist on supervisorctl settings, you know, run well in command line means it has nothing to do with code. So I refer to more questions. Now I find the answer. The answer is to add `environment=LANG=en_US.UTF-8, LC_ALL=en_US.UTF-8, LC_LANG=en_US.UTF-8` to the `[supervisord]` section of supervisor settings file. This is where I find the solution.[link](https://stackoverflow.com/questions/9208802/unicodeencodeerror-when-uploading-files-in-django-admin?rq=1). Anyway, thank you very much for your help! – Satori Komeiji Jul 20 '17 at 14:09