简体   繁体   中英

How to get textbox value in views.py django

I am trying to get the value of a textbox from views.py but its not working for some reason.

Below is my code

base.html

 <form method="POST" role="form" > {% csrf_token %} <div class="tab-content"> <div class="tab-pane active" role="tabpanel" id="step1"> <div class="mobile-grids"> <div class="mobile-left text-center"> <img src="{% static 'images/mobile.png' %}" alt="" /> </div> <div class="mobile-right"> <h4>Enter your mobile number</h4> <!-- <label>+91</label><input type="text" name="mobile_number" class="mobile-text" value="asdfasd" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '';}" required=""> --> <label>+91</label><input type="text" name="mobile_number" class="mobile-text" value="" > </div> </div> <ul class="list-inline pull-right"> <li><button type="button" class="mob-btn btn btn-primary btn-info-full" data-dismiss="modal">Finish</button></li> </ul> </div> <div class="clearfix"></div> </div> </form> 

views.py

def home(request):
    mobile_number = request.POST.get('mobile_number')
    print(mobile_number)
    return render(request,"home.html", {'mobile_number': mobile_number})

I am getting None when I try to get the value of textbox mobile_number .

How can I get the correct value?

You are missing a submit-button ( <input type="submit" value="submit"/> ) from the from.

To see the value on the form after submitting it, change value="" on the mobile_number -row, to value="{{ mobile_number }}" .

My solution 1. You have to set action in form field as your functon in view.py <form class="forms-sample" name="form" action="{% url "update_size" %}" method="post">

  1. You have make button type as submit.

    <button type="Submit" value="Submit" name="Submit" class="btn btn-primary">

  2. url.py

    path('updatesize/', views.updatesize, name='update_size')

  3. View.py def updatesize(request): if (request.method == 'POST'): temp = request.POST.get('size_name') print("==================form================",temp) return redirect("/collection.html")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM