Я создаю чат-бота, используя HTML-шаблон и django. Я могу получить пользовательский ввод из html в views.py, но когда мне не удается отправить вывод в тот же html.
Я попытался указать {{output}} в теге script. Но не сработало.
Мой код chatbox.html, включая jquery:
<html>
<body>
<form action="/chatbot/" method="post">
{% csrf_token %}
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).on("keypress", "input", function(e){
if(e.which == 13){
var inputVal = $(this).val();
document.getElementById("myTextarea").innerHTML += '\nMe: '+inputVal+'\n');
var outputVal = '{{output}}' || false;
function printText(){
document.getElementById("myTextarea").innerHTML += '\nBot: '+outputVal +'\n';
}
setTimeout(printText,500);
}
});
</script>
<textarea id="myTextarea" rows="30", cols="60", name="text-area">Hi dude! Welcome to Chat Bot</textarea>
<input id='myInput' name="utext" placeholder="Ask Something..." type="text">
</form>
</body>
</html>
Мой файл views.py:
from django.shortcuts import render, HttpResponse
def get_input(request):
print(request.POST)
input = request.POST.get('utext',False)
print("input:",input)
output = "Hello! How are you doing?"
return render(request, "chatbox.html", {'output': output})
Hi dude! Welcome to Chat Bot
Me: hi
Bot: Hello! How are you doing?
Этоожидаемый результат от этого представления.