Estoy trabajando en un chatbot y quiero implementarlo en Django pero estoy usando una interfaz independiente. Tengo poco o ningún conocimiento sobre JavaScript y copié este script de algún lado:
onSendButton(chatbox) { var textField=chatbox.querySelector('input'); let text1=textField.value if(text1==="") { return; } let msg1={name: "User", message: text1} this.messages.push(msg1); this.updateChatText(chatbox) textField.value='' fetch('http://localhost:8000/chat', { method: 'POST', body: JSON.stringify({message: text1}), mode: 'cors', headers: { 'Content-Type': 'application/json' }, }) .then(r => r.json()) .then(r => { console.log(r) let msg2={name: "CareAll", message: r.answer}; this.messages.push(msg2); if("follow_up" in r) { let msg3={name: "CareAll", message: r.follow_up}; this.messages.push(msg3) } this.updateChatText(chatbox) textField.value='' }).catch((error) => { console.error('Error:', error); this.updateChatText(chatbox) textField.value='' }); }Esta función es para / ruta de chat
def chat_bot_response(request): if request.method == "POST": u_msg = json.loads(request.body)["message"] ints = predict_class(u_msg, cb_model) resp = {"answer": getResponse(ints, intents)} maxConf = max(ints, key=lambda x: x["probability"]) if maxConf["intent"] not in [ "greeting", "farewell", "about_self", "about_self_function", "question", "unknown", "yes_to_symptom", "no_to_symptom"]: RESP_LIST.append(resp["answer"]) resp["answer"] = "Do You Have other symptoms?" if maxConf["intent"] == "yes_to_symptom": print(RESP_LIST) elif maxConf["intent"] == "no_to_symptom": resp["answer"] = RESP_LIST[0] return JsonResponse(resp)Lo que estoy tratando de hacer es permanecer en esta función cuando se abra el cuadro de chat hasta que el usuario abandone la página. ¿Tendré que mejorar JavaScript? usar la plantilla de Django? cual es la posible solucion.