2

我正在使用Flask并希望呈现 html 页面并直接关注特定的dom elementusing /#:id.

下面是默认/渲染的代码

@app.route('/')
def my_form():
    return render_template("my-form.html")

下面是根据POST请求调用的函数。

@app.route('/', methods=['POST'])
def my_form_post():
    #...code goes here
    return render_template("my-form.html")

我想渲染我的my-form.html页面,my-form.html/#output以便它应该直接关注elementdom 中的期望。

但是尝试return render_template("my-form.html/#output")告诉没有这样的文件并且尝试@app.route('/#output', methods=['POST'])也不起作用。

更新:

考虑这个网络应用JSON2HTML - http://json2html.herokuapp.com/

发生了什么:每当一个人单击send按钮时,文本区域输入和样式设置都会发送到我的 python 后端烧瓶应用程序,如下所示。

@app.route('/', methods=['POST'])
    def my_form_post():
        #...code for converting json 2 html goes here
        return render_template("my-form.html",data = processed_data)

我想要什么:每当send单击按钮POSTED并处理表单数据时,并且使用包含要显示的处理数据的新参数重定向同一页面。我的问题是呈现附加片段标识符的同一页面,#outputTable以便在转换后页面直接关注用户想要的所需输出。

4

1 回答 1

4

URL的片段标识符部分永远不会发送到服务器,因此 Flask 没有。这应该在浏览器中处理。在 Javascript 中,您可以将该元素作为window.location.hash.

如果您需要在服务器上进行突出显示,那么您需要寻找另一种方法来指示服务器接收到的突出显示内容,以便它可以将其提供给模板。例如:

# focus element in the query string
# http://example.com/route?id=123
@app.route('/route')
def route():
    id = request.args.get('id')
    # id will be the given id or None if not available
    return render_template('my-form.html', id = id)

这是另一种方式:

# focus element in the URL
#http://example.com/route/123
@app.route('/route')
@app.route('/route/<id>')
def route(id = None):
    # id is sent as an argument
    return render_template('my-form.html', id = id)

对编辑的响应:正如我上面所说,片段标识符由 Web 浏览器处理,Flask 永远不会看到它。首先,您必须<a name="outputTable">Output</a>在要跳转到的部分中添加模板。然后,您有两个选择: hacky 一个是编写action表单的属性,包括主题标签。更好的选择是添加一个onload在页面加载后跳转的 Javascript 事件处理程序。

于 2013-09-22T16:06:41.633 回答