Flask url_for URLs in Javascript

前端 未结 8 2071
粉色の甜心
粉色の甜心 2021-01-30 10:54

What is the recommended way to create dynamic URLs in Javascript files when using flask? In the jinja2 templates and within the python views url_for is used, what i

8条回答
  •  梦如初夏
    2021-01-30 11:11

    I use this dirty and ugly trick:

    "{{url_for('mypage', name=metadata.name,scale=93,group=2)}}"
    .replace('93/group',scale+'/group')
    

    where scale is the javascript variable I want to use for an AJAX request. So, url_for generate an URL and JavaScript replace the parameter at runtime. The generated code is something like:

    "/ajaxservive/mynam/scale/93/group/2".replace('93/group',scale+'/group')
    

    which is pretty strange, but still can't find more elegant solutions.

    In fact, looking at the code of the linked flask extension, it does the same thing but managing the general case.

提交回复
热议问题