A
A
Antonio Solo2018-06-23 21:03:03
Django
Antonio Solo, 2018-06-23 21:03:03

How to upload files with names in UTF-8 in Django Admin?

I ran into a problem: when uploading files to the server with Russian-language names, a
UnicodeEncodeError at /admin/dancercatalog/dancer/3/change/
'ascii' codec can't encode characters in position 78-84: ordinal not in range(128)
error occurs , everything works on a local machine under Windows
How to fix it ? Where to dig?

UnicodeEncodeError at /admin/dancerscatalog/dancer/3/change/
'ascii' codec can't encode characters in position 78-84: ordinal not in range(128)
Request Method:	POST
Request URL:	http://firesonu.beget.tech/admin/dancerscatalog/dancer/3/change/
Django Version:	2.0.6
Exception Type:	UnicodeEncodeError
Exception Value:	
'ascii' codec can't encode characters in position 78-84: ordinal not in range(128)
Exception Location:	/home/f/firesonu/.local/lib/python3.4/genericpath.py in exists, line 19
Python Executable:	/home/f/firesonu/.local/bin/python3
Python Version:	3.4.3
Python Path:	
['/home/f/firesonu/firesonu.beget.tech/firepalace',
 '/home/f/firesonu/.local/lib/python3.4/site-packages',
 '/home/f/firesonu/firesonu.beget.tech',
 '/opt/passenger40/helper-scripts',
 '/home/f/firesonu/.local/lib/python34.zip',
 '/home/f/firesonu/.local/lib/python3.4',
 '/home/f/firesonu/.local/lib/python3.4/plat-linux',
 '/home/f/firesonu/.local/lib/python3.4/lib-dynload',
 '/home/f/firesonu/.local/lib/python3.4/site-packages']

5b2e8a1a936fd899523143.png5b2e8b45ce274109791434.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-06-24
@solotony

This should not occur and it is better to find out the root cause, but a crutch is also possible:

from os.path import splitext
from transliterate import slugify

def slugify_upload(instance, filename):
    name, ext = splitext(filename)
    return slugify(name) + ext


class Dancer(models.Model):
    photo = models.ImageFiled(upload_to=slugify_upload, ...)
    ...

Library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question