Tayyab
Tayyab

Reputation: 109

how to access radio button selected value in Django

I am trying to access radio button selected value in my django view but it returns on instead of selected value.

Template form

<form class="col-md-8 mx-auto" action="upload" method="post" enctype="multipart/form-data">
          {% csrf_token %}
    <div class="form-group" id="div_id_sample" >
        <label for="id_sample" class="col-form-label  requiredField">Select File</label><br>
        <div class="btn btn-primary">
        <input type="file" name="sample" class="clearablefileinput" required id="id_sample" accept=".exe">
         </div>
     </div>
     <div class="form-group">
         <label for="radiogroup">Enforce Timeout</label><br>
         <div class="btn-group" data-toggle="buttons" id="radiogroup">
             <label class="btn btn-secondary">
             <input type="radio" name="timeoptions" id="option60" autocomplete="off" value="60"> 60 seconds
             </label>

             <label class="btn btn-secondary">
             <input type="radio" name="timeoptions" id="option120" autocomplete="off" value="120"> 120 seconds
             </label>

             <label class="btn btn-secondary active">
             <input type="radio" name="timeoptions" id="option180" autocomplete="off" checked value="180"> 180 seconds
             </label>
         </div>
     </div>
      <div class="form-group">
         <label for="radiogroup2">Select Machine:</label><br>
            <div class="btn-group" data-toggle="buttons" id="radiogroup2">
                <label class="btn btn-secondary">
                <input type="radio" name="machineoptions" id="machine1" autocomplete="off" value="0"> Windows XP
                </label>

                <label class="btn btn-secondary active">
                <input type="radio" name="machineoptions" id="machine2" autocomplete="off" value="1" checked> Windows 7
                </label>
            </div>
       </div>
      <button type="submit" class="btn btn-primary">Submit</button>
</form>

views.py in my views i am using ModelForm but not in my HTML and the reason behind that is i copied and pasted the htmlcode generated by the ModelForm. Reason is because i need to collect two more items that are not in my data MODEL so that why.


def upload(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
            file = request.FILES['sample']
            form = SampleForm(request.POST, request.FILES)
            if form.is_valid():
                new_sample = form.save(commit=False)
                new_sample.file_name = file.name
                new_sample.user = request.user
                print(request.POST)
                TimeOut = request.POST.get('timeoptions')
                machine = request.POST.get('machineoptions')

                print(TimeOut)
                print(machine)

                form.save()
                return redirect('reports')
            else:
                messages.info(request,'Invalid Form')
        else:
            form = SampleForm()
        if request.method == 'GET':
            return render(request, 'upload2.html', {'form': form})
    else:
        return redirect(reverse('login'))


Print output

<QueryDict: {'csrfmiddlewaretoken': ['VvAqdOp8RrpKOgwxLD2dpGartPvUrTHTg9AUbqsX6vphxdojTJqn7tsvLCkneWOm'], 'timeoptions': ['on'], 'machineoptions': ['on']}>
on
on

I am just trying to access the selected button value.

Upvotes: 3

Views: 6438

Answers (1)

Sammy J
Sammy J

Reputation: 1066

I guess this is mostly because you are enclosing the <input> tag inside the <label> tag.Also you must give the for attribute for and put the input element's id in the <label> tag

Try like this:

<label class="btn btn-secondary" for="option60">60 seconds</label>
<input type="radio" name="timeoptions" id="option60" autocomplete="off" value="60"> 

You can do this for all the radio buttons.Then in your view you can access with TimeOut = request.POST.get('timeoptions')

Upvotes: 3

Related Questions