0

So what I am trying to do is grab the users input, and then it append it to the HTML, I am making an attempt to make a chatbot system, I have the modules wrote I am just having trouble getting the users input. I have been looking around and I thought something like this would work but I am receiving back this:

{'rawText': None}

I will take the input, and pass it to the python module I wrote, and then append the results also to the HTML, like a messenger, still new to python, and django so confused as to why I am not getting any text back in console.

views.py:

from django.shortcuts import redirect, render
from django.shortcuts import HttpResponse
from .forms import messageForm
from .main import get_weather, take_user_input


def home(request):
    form = messageForm()

    # Get the message to display to view.
    if request.method == "POST":
        rawText = request.GET.get('textInput')
        context = {'rawText': rawText}
    print(context)
    return render(request, 'home.html', {'form': form})

forms.py:

    from django import forms
    
    
    class messageForm(forms.Form):
        message = forms.CharField(max_length=1000)

home.html:

{% extends 'base.html'%}
{% load static %}

{% block content %}
    <h2>Blue's Chatbot</h2>
    <div>
        <div id="chatbox">
            <p class="botText"><span>Hi! Let's get to chatting.</span></p>
        </div>
        <div id="userInput">
        <form type="text" id="textInput" action="" method="POST" novalidate>
            {% csrf_token %}
            {{ form }}
            <input id="buttonInput" type="submit" value="Send">
            </form>
        </div>

{% endblock %}

1 Answer 1

1

You're using POST method to send the data to the server so you should get the data from POST dictionary not GET:

request.POST.get('textInput', '') # instead of request.GET.get('textInput', '')
Sign up to request clarification or add additional context in comments.

3 Comments

That does make a lot of sense, but now I am getting ``` {'rawText': ''} ``` in the console.
That's because you don't have any input field with id "textInput" inside your form, "textInput" is the id of your form itself. You need to have an input box to get user's input and use the id of that input box.
I fixed it I have an input field, but I was using the wrong id for that, it was the name of the form variable. Inspect element came in handy :) - Will mark your answer as correct once I am able.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.