Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Django Keep connection with route through standalone frontend

I am working on a chatbot and want to deploy it in Django but I am using a standalone frontend. I have little to no knowledge about JavaScript and copied this Script from somewhere:

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=''
            });
    } 

This function is for /chat route

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)

What I am trying to do is, I want to stay in this function when the chatbox is opened until the user leaves the page. Will I need to improve JavaScript? use Django template? what is the possible solution.

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!