2

我使用Flask并且想要渲染html页面并直接关注特定的dom element使用/#:id渲染html页面/#: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所以它应该直接在DOM专注于所需element

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

UPDATE:

考虑这个web应用程序JSON2HTML - http://json2html.herokuapp.com/

发生了什么:每当一个人点击send按钮textarea的输入和造型设置发送到我的python后端flask-app如下。

@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和处理,并在同一个页面重定向与包含要显示的processed_data的新参数。我的问题是渲染同一页面附加片段标识符#outputTable,以便在转换后,页面直接关注用户所需的所需输出。

回答

3

fragment identifier 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浏览器处理,瓶永远看不到它。首先,您必须将<a name="outputTable">Output</a>添加到您要跳转到的部分中的模板。然后你有两个选择:hacky是编写你的表格的action属性,包括hashtag。更好的选择是添加一个onload Javascript事件处理程序,该处理程序在页面加载后跳转。

+0

Thanks @Miguel。请参阅最新的问题。我无法执行所需的操作。 – softvar

+0

我在您的更新中添加了其他评论。 – Miguel