How To Get Radio Button Value From Form In View.py File
I wanna get the radio button value in view.py file, I'm getting all the values except for radio button value I tried to get data using POST and GET method but both didnt work for m
Solution 1:
Do something like this.
forms.py
from django import forms
NUMS= [
('one', 'one'),
('two', 'two'),
('three', 'three'),
('four', 'four'),
('five', 'fives'),
]
classCHOICES(forms.Form):
NUMS = forms.CharField(widget=forms.RadioSelect(choices=NUMS))
views.py
from .forms import CHOICES
defname_of_url_goes_here(request):
form = CHOICES(request.POST)
if form.is_valid():
selected = form.cleaned_data.get("NUMS")
print(selected)
return render(request, 'name_of_page.html', {'form':form})
html
<formclass="form-inline"method='POST'action=""enctype='multipart/form-data'>{% csrf_token %}
{{form.NUMS}}
</form>
Solution 2:
The whole thing is done backwards. Post your views, forms, and html and I'll show you the right way. Basically, you shouldn't be naming all 5 of those fields the same way, and the radio button should be one field with the selections given in form.py. Then you would call form.cleaned_data.get("radoption") to get the info in views.py ...
Post a Comment for "How To Get Radio Button Value From Form In View.py File"