for仅传递第一个参数

for仅传递第一个参数

本文介绍了Flask URL路由:url_for仅传递第一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery选项卡式面板,可在单击时在选项卡中加载页面.这很好.我现在想要的是将其他参数传递给url,例如被单击的选项卡的标题.加载的脚本如下:

I have jQuery tabbed panel that loads pages within the tab on click. This works fine. What I want now is to pass additional arguments to the url such as the title of the tab being clicked.The script to load is the following:

$(document).ready(function(){
$( "#content" ).load( "{{url_for(xyz, query=query)}}" );
var tabs = $('.tabs > li');
tabs.on("click", function(){
  tabs.removeClass('active');
  $(this).addClass('active');
  $( "#content" ).load( "{{url_for(xyz, query=query)}}" );
})
});

html如下:

<ul class="tabs clearfix" >
<li>
  <a href=# >All</a>
</li>
<li class=active >
  <a href=# >You</a>
</li>
<li>
  <a href=# >Me</a>
</li>

我想将标签标题作为另一个参数传递给我的烧瓶url_for.另外,什么是检查对象属性的最佳方法.我的意思是,在python中,我可以使用dir(object)列出有关该对象的所有内容.有没有一种简单的方法来显示浏览器中某处被单击的选项卡对象的属性.谢谢

I would like to pass the tab title as another parameter to my flask url_for. Also, What is the best way to inspect the properties of objects. I mean, in python I can use dir(object) to list everything about that object. Is there a simple way of showing the attributes of a clicked tab object somewhere in the browser. Thanks

更新这是我想出的:

var index = $(this).children('a')[0].title;

如果您想要标签的标题或

If you want the title of the a tag Or

var index = $(this).children('a')[0].innerHTML;

现在的问题是我无法将此js变量传递给url_for函数.或者更确切地说,它在我看来在函数内部显示为无"

Now the problem is that I am unable to pass this js variable to the url_for function. Or rather it shows up as None inside the function in my view

更新两次我现在有条件的,因为据我了解,我无法将js变量传递给jinja.

UPDATE TWOI now have a conditional since as per my understanding I cannot pass js variable to jinja.

if(index=='a'){
  $( "#content" ).load( "{{url_for('xyz', index='a', query=query)}}" );
  }
  else if(index=='b'){
  $( "#content" ).load( "{{url_for('xyz', index='b', query=query)}}" );
  }
  else{
  $( "#content" ).load( "{{url_for('xyz', query=query)}}" );
  }

现在的问题是,flask仅传递了第一个参数,而第二个为None.因此,在if块中,当查询为None时,索引的值将通过,而在else块中,查询具有值.视图内部的功能是

The problem now is that flask only passes the first argument while the second is None. So in the if blocks the values of index goes through while the query is None while in the else block the query has a value. The function inside view is

@app.route('/xyz')
def xyz():
    query = request.args.get("query")
    index = request.args.get('index')

更新3 我已经弄清楚了问题所在.我正在使用jQuery load函数中的字符串来加载页面中的内容. url_for创建以下形式的网址

UPDATE 3I have figured out what the problem is. I am using the string inside jQuery load function to load up the content from the page. url_for creates a url of the form

/respanes?index=a&query=b

这是我所需要的,但是当它用引号引起来时,将&符转换为&amp;,浏览器无法正确读取

which is what I need but then when its enclosed in the quotation marks the ampersand is converted into &amp; which the browser does not read properly

/respanes?index=a&amp;query=b

所以现在我只需要能够正确地转义它即可.我试图将字符串包装在encodeURI中,但这没有帮助.

So now I need to just be able to escape it properly. I tried to wrap the string in encodeURI but that didnt help.

所以我最终不得不使用replace来完全拥有所需的字符串.

So I finally had to use replace to exactly have the string as I want it.

 $( "#content" ).load('{{url_for('.xyz, index=a, query=query)}}'.replace('&amp;','&'));

希望有人可以提出一种更好的方法.我只想拥有一个选项卡式面板,它将通过ajax在其中加载页面.我不确定是否需要做所有这些事情,但是希望像我这样的菜鸟可以从我的挣扎中收集:)

Hopefully someone can come up with a better way of doing this. All I wanted was to have tabbed panel that will load a page in it through ajax. i am not sure going through all this hoopla is what's required but hopefully some noob like me can glean from my struggles :)

推荐答案

我参加聚会有点晚了,但是我遇到了同样的问题.正确的解决方案非常简单:使用Jinja2 safe过滤器强制其输出原始字符串,而不是使用网络安全实体对其进行格式化.

I'm a little late to the party, but I encountered this same problem. The proper solution is pretty straightforward: use the Jinja2 safe filter to force it output the raw string instead of formatting it with web-safe entities.

{{url_for('x.y', index=a, query=query) | safe}}

有关过滤器的更多信息,请参见: http://jinja.pocoo. org/docs/dev/templates/#safe

More information on the filter is available here: http://jinja.pocoo.org/docs/dev/templates/#safe

这篇关于Flask URL路由:url_for仅传递第一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:28