python - Flask templating and redirect issue -
i have page can click pre-selected image or upload own image. send users new page after either of actions completed. same page both actions.
this code when user uploads image , subsequently sent new page, presents image:
@app.route('/', methods=['get', 'post']) def upload_file(): if request.method == 'post': file = request.files['file'] if file , allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['upload_folder'], filename)) return redirect(url_for('uploaded_file', filename=filename)) return render_template('index.html') @app.route('/photo/<filename>') def uploaded_file(filename): return render_template('uploaded.html', filename=filename)
to try , fit these images functions, tried changing url_for images, so:
<img href="{{ url_for('uploaded_file') }}" id="doge1" src="{{ url_for('static', filename='img/doge1.png') }}"> <img href="{{ url_for('uploaded_file') }}" id="doge2" src="{{ url_for('static', filename='img/doge2.jpg') }}"> <img href="{{ url_for('uploaded_file') }}" id="doge3" src="{{ url_for('static', filename='img/doge3.jpg') }}">
but met build error. assume because not uploading image.
how can redirect uploaded_file page clicking on 1 of images? nice if pass image name (i.e. doge1, doge2 or doge3) uploaded file function is, i'm not sure if that's possible.
here build error message:
werkzeug.routing.builderror builderror: ('uploaded_file', {}, none) traceback (most recent call last) file "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) file "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) file "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) file "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() file "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e) file "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb) file "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request rv = self.dispatch_request() file "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) file "/users/metersky/code/doge2/app.py", line 29, in upload_file return render_template('index.html') file "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template context, ctx.app) file "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 110, in _render rv = template.render(context) file "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render return self.environment.handle_exception(exc_info, true) file "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception reraise(exc_type, exc_value, tb) file "/users/metersky/code/doge2/templates/index.html", line 1, in top-level template code {% block content %} file "/users/metersky/code/doge2/templates/index.html", line 31, in block "content" <img href="{{ url_for('uploaded_file') }}" id="doge1" src="{{ url_for('static', filename='img/doge1.png') }}"> file "/usr/local/lib/python2.7/site-packages/flask/helpers.py", line 312, in url_for return appctx.app.handle_url_build_error(error, endpoint, values) file "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1641, in handle_url_build_error reraise(exc_type, exc_value, tb) file "/usr/local/lib/python2.7/site-packages/flask/helpers.py", line 305, in url_for force_external=external) file "/usr/local/lib/python2.7/site-packages/werkzeug/routing.py", line 1616, in build raise builderror(endpoint, values, method) builderror: ('uploaded_file', {}, none)
you not passing input parameter ’’’uploaded_file’’’ therefore error. call this:
href="{{ url_for('uploaded_file', filename='whatever') }}" id="doge1" src="{{ url_for('static', filename='img/doge1.png') }}">
Comments
Post a Comment