2024-05-19 06:22:49 发布
网友
我正在开发一个Django应用程序,我需要在前端(javascript)中定义的url字符串中插入一个变量,以便从用户的文件夹中获取json文件:
'{% static "users/ userName /file.json" %}'
我希望使用userName作为变量。我需要这个jstree插件的url。我必须显示用户的文件夹树:
userName
我尝试了不同的语法,但到目前为止还没有成功。你知道解决这个问题的办法吗?在
非常感谢你的帮助!在
jsperf comparison中提到了三种可能的字符串连接方法:
连接字符串的最佳性能方法是使用assignment(^{cd1>},+=)运算符:
+=
'{% static "users/' + userName + '/file.json" %}'
次优选项(至少在较新的浏览器中)与concat()函数连接:
The concat() method combines the text of one or more strings and returns a new string.
concat()
使用此方法的解决方案如下所示:
''.concat(['{% static "users/', userName, '/file.json" %}'])
第三个选项包括使用join()函数:
The join() method joins all elements of an array (or an array-like object) into a string and returns this string.
join()
使用此方法的字符串格式如下所示:
['{% static "users/', userName, '/file.json" %}]'.join()
请注意,这次串联是从字符串数组而不是字符串数组执行的。
例如,如果userName应该等于'FooBar',那么输出将是
'FooBar'
'{% static "users/JonDoe/file.json" %}'
jsperf comparison中提到了三种可能的字符串连接方法:
连接字符串的最佳性能方法是使用assignment(^{cd1>},
+=
)运算符:次优选项(至少在较新的浏览器中)与concat()函数连接:
使用此方法的解决方案如下所示:
第三个选项包括使用join()函数:
使用此方法的字符串格式如下所示:
请注意,这次串联是从字符串数组而不是字符串数组执行的。
例如,如果
userName
应该等于'FooBar'
,那么输出将是相关问题 更多 >
编程相关推荐