【问题标题】:how to access radio button selected value in Django如何在 Django 中访问单选按钮选定的值
【发布时间】:2020-02-16 18:44:31
【问题描述】:

我试图在我的 django 视图中访问单选按钮选定的值,但它返回 on 而不是选定的值。

模板表单

<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 在我看来,我使用的是 ModelForm,但不在我的 HTML 中,其背后的原因是我复制并粘贴了 ModelForm 生成的htmlcode。原因是因为我需要再收集两个不在我的数据模型中的项目,这就是为什么。


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'))


打印输出

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

我只是想访问选定的按钮值。

【问题讨论】:

    标签: python html django radio-button


    【解决方案1】:

    我想这主要是因为您将&lt;input&gt;标签包含在&lt;label&gt;标签内。另外,您必须提供for属性for并将输入元素的id放入&lt;label&gt;标签中

    试试这样:

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

    您可以对所有单选按钮执行此操作。然后在您的视图中,您可以使用TimeOut = request.POST.get('timeoptions') 访问

    【讨论】:

    • 嗨,萨米。我得到了“开”这个词。我可以访问所选按钮的值吗?
    猜你喜欢
    • 2013-02-08
    • 2021-04-14
    • 1970-01-01
    • 2014-09-09
    • 2020-11-12
    • 2013-04-03
    • 2018-12-16
    • 2013-05-08
    • 2021-12-23
    相关资源
    最近更新 更多