Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在url字符串中插入一个变量-Django应用程序_Javascript_Python_Django_Url - Fatal编程技术网

Javascript 在url字符串中插入一个变量-Django应用程序

Javascript 在url字符串中插入一个变量-Django应用程序,javascript,python,django,url,Javascript,Python,Django,Url,我正在开发Django应用程序,需要在前端(javascript)中定义的url字符串中插入一个变量,以便从用户文件夹中获取json文件: '{% static "users/ userName /file.json" %}' 我希望将userName作为变量。我需要这个jstree插件的url。我必须显示用户的文件夹树: userFolder = '{% static "users/userName/file.json" %}' $('#jstree').jstree({ "co

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

'{% static "users/ userName /file.json" %}'
我希望将
userName
作为变量。我需要这个jstree插件的url。我必须显示用户的文件夹树:

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

 $('#jstree').jstree({
   "core" : {
   "animation" : 0,
   "check_callback" : true,
   "themes" : { "stripes" : true },
   'data' : {
   "url" : userFolder,

      ... more code ...
   }) 
我尝试了不同的语法,但到目前为止运气不好。你知道有什么解决办法吗


非常感谢你的帮助

中提到了三种可能的字符串连接方法:

  • 串接字符串的最佳性能方法是使用(
    +
    +=
    )运算符:

    '{% static "users/' + userName + '/file.json" %}'
    
  • 第二个最佳选项(至少在较新的浏览器中)是连接函数:

    concat()
    方法组合一个或多个字符串的文本并返回一个新字符串

    使用此方法的解决方案如下所示:

    ''.concat(['{% static "users/', userName, '/file.json" %}'])
    
    ['{% static "users/', userName, '/file.json" %}]'.join()
    
  • 第三个选项将包括使用以下功能:

    join()
    方法将数组(或类似数组的对象)的所有元素连接到一个字符串中,并返回该字符串

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

    ''.concat(['{% static "users/', userName, '/file.json" %}'])
    
    ['{% static "users/', userName, '/file.json" %}]'.join()
    
    请注意,这次连接是从字符串数组而不是字符串数组执行的

例如,如果您希望
用户名
应等于
'FooBar'
,则输出将为

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

嗨,谢谢你的回答。不幸的是,我仍然得到一个错误:SyntaxError:identifier在numeric literal之后立即启动。@Sim81我提到的所有方法在Python3和Python2中都运行良好。也许你应该更具体地描述你的问题?您试图在什么上下文中使用字符串?你期望什么样的产出?尝试更新您的描述。@Sim81是否可以使用包含的库和代码行更新您的问题?我可以自己测试。@Lycopersicum我添加了一些额外的代码。我需要一个jstree插件的url。谢谢大家!@Sim81我将在几个小时内尝试改进我的答案,目前我无法使用python pip,因为没有足够的规则。