Flask загрузка не может обслуживать файлы изображений с еврейскими буквами в имени файла - PullRequest
0 голосов
/ 09 марта 2020

Моя проблема заключается в том, что при загрузке Flask не удалось отправить файлы изображений с еврейскими буквами в имени файла. Это моя форма:

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, TextAreaField
from wtforms.validators import InputRequired, Length
from flask_wtf.file import FileField, FileAllowed
from flask_uploads import IMAGES



class RegisterForm(FlaskForm):
    name = StringField('Full name', validators=[InputRequired('A full name is required'),
                                                Length(max=100, message='your name can\'t be more than 100 characters')])
    username = StringField('Username', validators=[InputRequired('A username is required'),
                                                   Length(max=100, message='your username can\'t be more than 30 characters')])
    password = PasswordField('Password', validators=[
                             InputRequired('A password is required')])

    image = FileField(validators=[FileAllowed(IMAGES, 'Only images accepted')])

Я проверил ее, и поле изображения было принято даже с еврейскими буквами в имени файла. Но с точки зрения есть проблема. Конфигурация загрузки flask:

photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos) 

И само представление:

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        image_filename = photos.save(form.image.data)
        image_url = photos.url(image_filename)

        new_user = User(name=form.name.data, username=form.username.data,
                        password=generate_password_hash(form.password.data),
                        image=image_url, join_date=datetime.now())

        db.session.add(new_user)
        db.session.commit()

        return redirect(url_for('profile'))

    return render_template('register.html', form=form) 

Если в имени файла есть буквы Engli sh, это хорошо работает, но если есть В еврейских буквах есть ошибка с линией:

image_filename = photos.save(form.image.data)

Ошибка:

Traceback (most recent call last):
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/yosef/Desktop/twitee/views.py", line 97, in register
    image_filename = photos.save(form.image.data)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask_uploads.py", line 415, in save
    raise UploadNotAllowed()

Как с этим справиться?

...