Getting user input from a input field in Django

Question:

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 %}
Asked By: Blue

||

Answers:

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', '')
Answered By: Ashish Ranjan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.