Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'SpooledTemporaryFile' object has no attribute 'replace'

Tags:

flask

upload

I'm using flask to make a web to upload videos, adding a video worked but when I try to edit(replace) the video I uploaded, there's a AttributeError: 'SpooledTemporaryFile' object has no attribute 'replace'

In forms.py, url as the route of the video:

url = FileField(
    label="Video",
    validators=[
        Optional()
    ],
    description="Video"

In views,py:

def movie_edit(id=None):
    form = MovieForm()
    form.url.validators = []
    movie = Movie.query.get_or_404(int(id))
    if form.validate_on_submit():
        data = form.data
        if not os.path.exists(app.config["UP_DIR"]):
            os.makedirs(app.config["UP_DIR"])
            os.chmod(app.config["UP_DIR"], "rw")
        if data["url"] != "":
            file_url = secure_filename(data["url"])
            movie.url = change_filename(file_url)
            form.url.data.save(app.config["UP_DIR"] + movie.url)

How to fix this error?

like image 547
Klawens Avatar asked Mar 18 '26 12:03

Klawens


1 Answers

I'm guessing that data["url"] is a SpooledTemporaryFile object, not a string. Without the full stacktrace, I can't tell which line is barfing, but you probably need to change file_url = secure_filename(data["url"]) to file_url = secure_filename(data["url"].filename) or something similar.

like image 166
Kirk Avatar answered Mar 24 '26 03:03

Kirk