在url字符串Django App中插入变量

2024-05-19 06:22:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个Django应用程序,我需要在前端(javascript)中定义的url字符串中插入一个变量,以便从用户的文件夹中获取json文件:

'{% static "users/ userName /file.json" %}'

我希望使用userName作为变量。我需要这个jstree插件的url。我必须显示用户的文件夹树:

^{pr2}$

我尝试了不同的语法,但到目前为止还没有成功。你知道解决这个问题的办法吗?在

非常感谢你的帮助!在


Tags: 文件django字符串用户文件夹json应用程序url
1条回答
网友
1楼 · 发布于 2024-05-19 06:22:49

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(['{% 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.

    使用此方法的字符串格式如下所示:

    ['{% static "users/', userName, '/file.json" %}]'.join()
    

    请注意,这次串联是从字符串数组而不是字符串数组执行的。

例如,如果userName应该等于'FooBar',那么输出将是

'{% static "users/JonDoe/file.json" %}'

相关问题 更多 >

    热门问题