John
John

Reputation: 69

django read radio value from a normal form

my form code is :

<td class="desc"><label>Description:<input type="text" name="desc"></label></td>
    <td class="tags"><label>Tags:<input type="text" name="tag"></label></td>
<td><label>Public:<input type="radio" name="pri" value="1"></label>
<label>Private:<input type="radio" name="pri" value="0"></label></td>   

in views.py

meta['pri'] = request.POST.get('pri','')   

why i choose the Private button, the value "pri" always is the "1"

Upvotes: 3

Views: 5862

Answers (3)

user2056721
user2056721

Reputation: 1

Only solution for this is using onclick event on radio button and writing to hidden input with another name, then working with this input in views

Upvotes: 0

Ryan
Ryan

Reputation: 31

I think you can get the result like this:

meta['pri'] = bool(request.POST.get('pri')=='1')

Upvotes: 1

CJ4
CJ4

Reputation: 2535

You are selecting by name, so the first element with the name 'pri' is selected, which will always be the label element with the name 'pri' and a value of 1. The name of the label should be different to the name of the input element.

Upvotes: 4

Related Questions