简体   繁体   中英

How to pass value from html file to views.py file?

after user enter 10 digits number automatically python function will call. i need to send text box value to python function (views.py). How to get value in views.py?

i don't want when user click on submit button to get value in text box.

base.html

  <input type="text" name="vat" id="vat1" placeholder="Please Enter 10 digits number">

script

var Value = document.getElementById('vat1').value;
alert(Value);

views.py

def call_operator(request):
    text_box_value = request.POST.get('vat')
    print(text_box_value)
    print("get operator name and circle name")
    template_name = 'mobile_recharge.html'
    extended_template = 'base.html'
    if request.user.is_authenticated():
        extended_template = 'base_login.html'
    return render(
        request, template_name,
        {'extended_template': extended_template}
    )

urls.py

url(r'^call_operator/$', call_operator)

script

<script type="text/javascript">
    $(document).ready(function(){
        $('#vat1').keyup(function(){
            if ($(this).val().length == 10){
                $('your_form').submit();
                 alert("condition satisfied");
             $.ajax({
                    type: "GET",
                    url: "http://localhost:8090/call_operator/"
                    });
            }
        });
    });
</script>

html file

          <form method="POST" role="form" name="your_form">
              <input type="text" name="vat" id="vat1" placeholder="Please Enter 10 digits number">
         </form>

You can use Ajax. This is other way to do it

base.html

<form method="POST" name="your_form" action="{% url 'your_app:your_url' %}">
    <input type="text" name="vat" id="vat1" placeholder="Please Enter 10 digits number">
</form>
<script type="text/javascript">
    $(document).ready(function(){
        $('#vat1').keyup(function(event){
            if (event.which == 13 || event.keyCode == 13){
                if ($(this).val().length == 10){
                    $('your_form').submit();
                }else{
                    // Show message if the text length is lesser than 10
                }
            }
        });
    });
</script>

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