Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用flask呈现html页面/#:id_Javascript_Python 2.7_Flask_Fragment Identifier_Html Rendering - Fatal编程技术网

Javascript 使用flask呈现html页面/#:id

Javascript 使用flask呈现html页面/#:id,javascript,python-2.7,flask,fragment-identifier,html-rendering,Javascript,Python 2.7,Flask,Fragment Identifier,Html Rendering,我使用的是Flask,希望使用/:id呈现html页面并直接关注特定的dom元素 下面是默认/呈现的代码 @app.route('/') def my_form(): return render_template("my-form.html") 下面是请求时调用的函数 @app.route('/', methods=['POST']) def my_form_post(): #...code goes here return render_template("my-fo

我使用的是
Flask
,希望使用
/:id
呈现html页面并直接关注特定的
dom元素

下面是默认
/
呈现的代码

@app.route('/')
def my_form():
    return render_template("my-form.html")
下面是请求时调用的函数

@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中所需的
元素

但是尝试
return render_template(“my form.html/#output”)
告诉我们没有这样的文件,而尝试
@app.route('/#output',methods=['POST'])
也不起作用

更新:

考虑此web应用程序JSON2HTML-

正在发生的事情:每当有人单击
发送
按钮时,文本区域输入和样式设置将发送到我的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)
我想要的:每当单击
发送
按钮,表单数据
发布
并进行处理时,同一页面将被重定向到包含要显示的已处理数据的新参数。我的问题是在同一个页面上添加片段标识符
#outputTable
,这样在转换后,页面会直接关注用户想要的输出。

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)
#URL中的焦点元素
#http://example.com/route/123
@app.route(“/route”)
@app.route(“/route/”)
def路由(id=无):
#id作为参数发送
返回呈现模板('my-form.html',id=id)
对编辑的响应:如上所述,片段标识符由web浏览器处理,Flask从未看到它。首先,您必须将
输出
添加到要跳转到的部分的模板中。然后您有两个选项:一个是编写表单的
action
属性,包括hashtag。更好的选择是添加一个
onload
Javascript事件处理程序,该处理程序在页面加载后跳转。

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)
#URL中的焦点元素
#http://example.com/route/123
@app.route(“/route”)
@app.route(“/route/”)
def路由(id=无):
#id作为参数发送
返回呈现模板('my-form.html',id=id)

对编辑的响应:如上所述,片段标识符由web浏览器处理,Flask从未看到它。首先,您必须将
输出
添加到要跳转到的部分的模板中。然后您有两个选项:一个是编写表单的
action
属性,包括hashtag。更好的选择是添加一个
onload
Javascript事件处理程序,该处理程序在页面加载后跳转。

谢谢@Miguel。请查看更新的问题。我无法执行所需的操作。谢谢@Miguel。请查看更新的问题。我无法执行所需的操作。