Javascript 如何通过html将flask base_url发送到js文件
我必须向Javascript 如何通过html将flask base_url发送到js文件,javascript,html,flask,Javascript,Html,Flask,我必须向js文件发送一个flask函数的base\u url,因为url是动态的 我能够为一个特殊的flask函数创建base\u url,比如 url_of_this_function = request.base_url root_url = url_of_this_function[:url_of_this_function.rfind('/')] pointing_url = str(url_for('filter_student')) filter_pointing_url =
js
文件发送一个flask函数的base\u url
,因为url是动态的
我能够为一个特殊的flask函数创建base\u url
,比如
url_of_this_function = request.base_url
root_url = url_of_this_function[:url_of_this_function.rfind('/')]
pointing_url = str(url_for('filter_student'))
filter_pointing_url = ''.join([root_url,pointing_url])
我通过render\u模板
return render_template('mypage.html',\
student_filter_link=filter_pointing_url,student_state_list=state_list)
mypage.html
<select id='student_state_selected' style='display:inline-block' onchange='get_student_filters({{ student_filter_link }})'>
<option disabled selected>Select</option>
{% for item in student_state_list %}
<option value="{{item}}">{{item}}</option>
{% endfor %}
</select>
这是将url传递到js
的正确方法吗?
有没有更好的方法,或者我做错了什么?
<select id='student_state_selected' style='display:inline-block' onchange='get_student_filters({{ student_filter_link }})'>
<option disabled selected>Select</option>
{% for item in student_state_list %}
<option value="{{item}}">{{item}}</option>
{% endfor %}
</select>
挑选
{学生\状态\列表%中的项目的%
{{item}}
{%endfor%}
我只需要将onchange='get_student_filters({{{student_filter_link}}})
更改为onchange='get_student_filters({{student_filter_link}')
成功了
希望它对某人有所帮助。Hi,在你的HTML文件中添加一个脚本标记,并在其中编写以下js代码
const student\u filter\u link={{student\u filter\u link}
我认为最好在头中使用HTML标记,比如标记。因此,您可以使用Js中的document.getElementsByTagName(“base”)[0].href
获取url。但不要把它当作一个建议,而是作为一个备选方案:)
<select id='student_state_selected' style='display:inline-block' onchange='get_student_filters({{ student_filter_link }})'>
<option disabled selected>Select</option>
{% for item in student_state_list %}
<option value="{{item}}">{{item}}</option>
{% endfor %}
</select>