A
A
Artyom Belousov2017-08-06 23:55:48
Django
Artyom Belousov, 2017-08-06 23:55:48

How to get rid of the error when uploading files with a Russian name?

Apache server. When I want to upload a file through a form (or admin panel), which has Russian letters in the name, the server issues a 500th error. When uploading files without Cyrillic, everything is fine.
models.py

class Book(models.Model):
    name = models.CharField(max_length=50, verbose_name='Название')
    author = models.ForeignKey(Author, verbose_name='Автор')
    image = models.ImageField(verbose_name='Изображение')
    short_description = models.TextField(default='', verbose_name='Краткое описание')
    year = models.IntegerField(verbose_name='Год')
    text = models.FileField(verbose_name='Текст книги')
    tags = TaggableManager(verbose_name='Жанр(ы)')

    class Meta:
        verbose_name = 'Книга'
        verbose_name_plural = 'Книги'

    def __str__(self):
        return f"{self.name} {self.year}"

forms.py
class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = ('name', 'image', 'short_description', 'year', 'text', 'tags')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JonGalt, 2017-08-07
@flygrounder

Use the pytils library for transliteration

from pytils import translit   

class Book(models.Model):
     
   def get_image_path(self, filename):
           path = ''.join(["pictures/",translit.slugify(filename)])
           return path
       ...
     image = models.ImageField(verbose_name='Изображение', upload_to=get_image_path)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question