B
B
bituke2021-06-11 06:44:11
JavaScript
bituke, 2021-06-11 06:44:11

How to use django template engine in js code?

I wrote my first chat in django + wb, I need the name of the user who sent it to be displayed next to the message. The js syntax does not allow the use of a template engine, and if somehow perverted, it still does not work.
js:

var a = '{{ request.user }}';

webSocket.onmessage = function(e) {
    const data = JSON.parse(e.data);
    chat.innerHTML += '<div style="color:red" class="msg">' + a + 'Сообщение от автора <br>' + data.message + '</div>'
};


What it looks like:
60c2dbc7a139d860577970.png
That is, it simply does not accept data from the back, it does not work.
How can this be implemented so that the name of the current user who sent the message would be displayed? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Markov, 2021-06-11
@bituke

This should be specified in the template, not in the script file.

<script>
  var a = '{{ request.user }}';
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question