简体   繁体   中英

How can I get value of radio button's input?

How can I get the value of an input? I wrote the code in HTML,

<form action="{% url 'app:top' %}" method="POST">
              <input type="radio" name="no" value="1">
              <label>No</label>
              <input type="radio" name="yes" value="1">
              <label>Yes</label>
<input class="disabled" type="submit" value="POST">
</form>

in views.py ;

def top(request):
    no = request.POST.get("no","0")
    yes = request.POST.get("yes","0")

    print(no)
    print(yes)

When I No label and put send button, 0 is printed. Putting Yes button is same. Why does it happens? Why can't I print out 1? How should I do it?

<form action="{% url 'app:top' %}" method="POST">
          <input type="radio" name="answer" value="no">
          <label>No</label>
          <input type="radio" name="answer" value="yes">
          <label>Yes</label>
<input type="submit" value="POST">
</form>

In views.py

def top(request):
    answer = request.POST.get('answer')
    print(answer)

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