0

I have a file upload form in HTML, which should be used to upload and save to a static folder. The problem is that it works fine in a simple Flask app, but it just doesn't work using Blueprint templates.

I have tried several different ways of making this work. When I use "url_for", the page doesn't even load (werkzeug.routing.BuildError: Could not build url for endpoint 'upload'. Did you mean 'forms_blueprint.upload' instead?).

But when I change "url_for" to "form_upload.html", the result is always that the method is not allowed.

All thoughts are appreciated.

routes.py:

UPLOAD_FOLDER = '/home/juliosouto/flask-gentelella/app/uploaded_files/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


@blueprint.route('/<template>', methods = ['GET', 'POST'])
@login_required
def route_template(template):
    return render_template('form_upload' + '.html')


@blueprint.route('/upload', methods=['POST','GET'])
def upload():
    file = request.files['file']
    filename = secure_filename(file.filename)
    file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

form_upload.html:

<form action ="{{ url_for('upload') }}" class= "dropzone" name="file" method = "POST" enctype = "multipart/form-data"></form>

I need the file to be saved to a static folder.

Julio S.
  • 944
  • 1
  • 12
  • 26
  • your upload route defaults to a GET request, but does not seem to provide a render_template or return. Also, you would want to do something like if request.method == Post before handling the file upload and then return to Get method or any other route. – gittert Feb 12 '19 at 12:29
  • @gittert, I have tried those too. None worked. – Julio S. Feb 12 '19 at 12:41

1 Answers1

1

When I use "url_for", the page doesn't even load (werkzeug.routing.BuildError: Could not build url for endpoint 'upload'. Did you mean 'forms_blueprint.upload' instead?).

This means exactly what it says. when you build the URL you need to do it as follows:

{{ url_for('forms_blueprint.upload') }}

Also, as @gittert correctly states, your upload route returns nothing.

This should render a template, or return a redirect to someplace else.

v25
  • 7,096
  • 2
  • 20
  • 36