Reputation: 12509
I have a page where you can click a pre-selected image or upload your own image. I would like to send users to a new page after either of those actions is completed. It would be the same page for both actions.
This is the code for when a user uploads an image and is subsequently sent to the new page, which presents the image:
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and 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 and fit these images into those functions, I tried changing the url_for for the images, like 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 I was met with a build error. I assume it is because I am not uploading an image.
How can I redirect to the uploaded_file page by clicking on one of my images? It would be nice if I could pass the image name (i.e. doge1, doge2 or doge3) to the uploaded file function where is, but I'm not sure if that's possible.
Here is the 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)
Upvotes: 0
Views: 858
Reputation: 5154
You are not passing the input parameter for the ’’’uploaded_file’’’ therefore you get the error. Call it like this:
href="{{ url_for('uploaded_file', filename='whatever') }}" id="doge1" src="{{ url_for('static', filename='img/doge1.png') }}">
Upvotes: 1