Answer the question
In order to leave comments, you need to log in
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>'
};
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question