简体   繁体   English

烧瓶模板和重定向问题

[英]Flask templating and redirect issue

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: 为了使这些图像适合这些功能,我尝试更改图像的url_for,如下所示:

<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? 如何通过单击我的其中一张图像重定向到upload_file页面? It would be nice if I could pass the image name (ie doge1, doge2 or doge3) to the uploaded file function where is, but I'm not sure if that's possible. 如果可以将图像名称(即doge1,doge2或doge3)传递到上载的文件函数中,那将是很好的,但是我不确定是否可以。

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)

You are not passing the input parameter for the '''uploaded_file''' therefore you get the error. 您没有传递'''uploaded_file'''的输入参数,因此收到错误消息。 Call it like this: 这样称呼它:

href="{{ url_for('uploaded_file', filename='whatever') }}" id="doge1" src="{{ url_for('static', filename='img/doge1.png') }}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM