JS доступ к python переменным через формат flask {{переменная}} не работает - PullRequest
0 голосов
/ 25 мая 2020

python:

import json
posts = [
{'author':'JL Rowling','title':'Harry Potter'},
{'author':'JRR Tolkien','title':'Lord of the Rings'},
]
encoded_posts = json.dumps(posts)

javascript:

let obj = {{ encoded_posts }};

JS ошибка в консоли:

Uncaught SyntaxError: Unexpected token ';'

Вот страница согласно КОНСОЛИ. Вроде теряет (?) {{encoded_posts}}.

<!DOCTYPE html>
<html>
  <head>

      <title>Flask Blog - About</title>

  </head>
  <body>
    <h1>About page!</h1>
    <script>
      let text = document.getElementsByTagName('h1')[0];
      text.addEventListener('click',function(){
        text.style.color = 'red';
      });

      let obj = ;
      let jsonPosts = JSON.parse();
      console.log(jsonPosts);

    </script>
  </body>
</html>

Что происходит?

1 Ответ

1 голос
/ 25 мая 2020

Вы пробовали делать:

let obj = JSON.parse('{{ encoded_posts }}');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...