Home > other >  How to pass data from html template (using html form) to views.py python in django?
How to pass data from html template (using html form) to views.py python in django?

Time:02-15

I am trying to get input from a html form to python views.py file in django. Below is my html and python code:

<form action="getsummary" method="post" enctype="multipart/form-data">
    <div >
      <label for="exampleFormControlTextarea1">Paste your text here</label>
      <textarea  name="inputText" id="exampleFormControlTextarea1" rows="10" style="width:1000px; "></textarea>
    </div>
</form>

<div >
    <div  style="width:160px">
        <a href="getsummary">
            <button type="submit" >Summarize Now</button>
        </a>
    </div>
</div>

def getsummary(request):
    if request.method == "POST":
        title = request.POST.get('inputText')
        print(title)
    return render(request, 'get-summary.html' )

I gave input text in my html form. My terminal was supposed to print that text but instead its printing Nothing.

[15/Feb/2022 12:20:33] "GET /summary HTTP/1.1" 200 2514
[15/Feb/2022 12:20:36] "GET /getsummary HTTP/1.1" 200 1961

I also tried doing it with GET method. In that case, it is printing None.

Kindly help me with this. Same issue I am facing when I am using ajax to pass javascript variable to views.py python file.

Thank you.

CodePudding user response:

Put the button component in the form tag and then try again

<form action="getsummary" method="post" enctype="multipart/form-data">
<div >
  <label for="exampleFormControlTextarea1">Paste your text here</label>
  <textarea  name="inputText" id="exampleFormControlTextarea1" rows="10" style="width:1000px; "></textarea>
</div>
<div >
  <div  style="width:160px">
    <a href="getsummary">
        <button type="submit" >Summarize Now</button>
    </a>
  </div>
</div>
</form>
  • Related