Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Problem with file uploading #460

Closed
akolesnikov93 opened this Issue · 3 comments

5 participants

@akolesnikov93

Strange error happened to 100% working code (I mean this code has been working brilliantly for a while, but now gives strange errors).
Using flask (0.7.2), flask-wtf, flask-uploads.

Main code:

@app.route('/task/new', methods=['GET', 'POST'])
def task():
    form = TaskForm(request.form)
    if request.method == 'POST' and form.validate():
        print type(form.image) # <class 'flaskext.wtf.file.FileField'>
        print type(form.image.file) # <type 'unicode'>
        print type(form.image.data) # <type 'unicode'>
        print form.image.file.filename # gives AttributeError: 'unicode' object has no attribute 'filename'

Some part from forms.py:

from flaskext.wtf import ....... FileField,  file_allowed
from flaskext.uploads import UploadSet, IMAGES

images = UploadSet("images", IMAGES)

class TaskForm(Form):
    url = TextField(u'URL', [validators.Length(min=4, max=400), validators.Required()])
    title = TextField(u'TITLE', [validators.Length(min=4, max=4000), validators.Required()])
    description = TextAreaField(u'Desc', [validators.Length(min=4, max=4000), validators.Required()])
    image = FileField(u'Image', [file_allowed(images, "Images only!")])

Jinja 2 template:

<form action="{{ url_for('task', task_id=task.id) }}" method="POST" enctype="multipart/form-data">
    {{ form.hidden_tag() }}
    {{ help.render_field(form.url) }} # using rendering form macro, no problem there
    {{ help.render_field(form.title) }}
        {{ help.render_field(form.description) }}
        {{ help.render_field(form.image) }}
        <button class="positive" type="submit">Save</button>
</form>

1) In Firefox submitting this form makes required fields (url, title, description) giving error "This field is required. " (Surely they are filled correctly).
2) In Chrome it's OK, but (from the main code):

    print type(form.image) # <class 'flaskext.wtf.file.FileField'>
    print type(form.image.file) # <type 'unicode'>
    print type(form.image.data) # <type 'unicode'>
    print form.image.file.filename # gives AttributeError: 'unicode' object has no attribute 'filename'

Enctype is correct (it was working well, no changes were made).
So what can I do with this errors? It was working great, but now crashes. If needed, hosting is ep.io

@justjkk

I'm having the same issue with Flask==0.8, Flask-Uploads==0.1.3 and Flask-WTF==0.6

@redouane

change
form = TaskForm(request.form)
to
form = TaskForm()

flask-wtf fills the form with data from request.form and request.files automatically, if you pass request.form, it will only process that and wont fill the form with request.files,
here's the code bit that does that:
https://bitbucket.org/danjac/flask-wtf/src/5eac9fb9ff93/flaskext/wtf/__init__.py#cl-140

@keyan
Collaborator

@untitaker I'm suggesting this be closed, it looks like @akolesnikov93 simply had a bug in their code.

@untitaker untitaker closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.