Home > Enterprise >  Getting user input from a input field in Django
Getting user input from a input field in Django

Time:07-25

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 ><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 %}

CodePudding user response:

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', '')
  • Related