noobmaster
noobmaster

Reputation: 157

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

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 class="form-group">
      <label for="exampleFormControlTextarea1">Paste your text here</label>
      <textarea class="form-control" name="inputText" id="exampleFormControlTextarea1" rows="10" style="width:1000px; "></textarea>
    </div>
</form>

<div class="d-flex justify-content-center">
    <div class="border d-flex justify-content-center" style="width:160px">
        <a href="getsummary">
            <button type="submit" class="btn btn-primary">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.

Upvotes: 0

Views: 1428

Answers (1)

Anar Ali
Anar Ali

Reputation: 586

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

<form action="getsummary" method="post" enctype="multipart/form-data">
<div class="form-group">
  <label for="exampleFormControlTextarea1">Paste your text here</label>
  <textarea class="form-control" name="inputText" id="exampleFormControlTextarea1" rows="10" style="width:1000px; "></textarea>
</div>
<div class="d-flex justify-content-center">
  <div class="border d-flex justify-content-center" style="width:160px">
    <a href="getsummary">
        <button type="submit" class="btn btn-primary">Summarize Now</button>
    </a>
  </div>
</div>
</form>

Upvotes: 1

Related Questions