python - 如何在 Django 中访问单选按钮选择的值

标签 python html django radio-button

我正在尝试访问我的 Django View 中的单选按钮选定值,但它返回 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 生成的 html 代码。原因是因为我需要收集另外两个不在我的数据模型中的项目,所以为什么。


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

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

最佳答案

我想这主要是因为您附上了 <input> <label> 内的标签tag.Also 你必须给 for 属性 for并把输入元素的 id<label>标记

像这样尝试:

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

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

关于python - 如何在 Django 中访问单选按钮选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58473385/

相关文章:

django - 如何关闭 oTree 中的 Debug模式?

Django、React、Redux : Serving up images

django - SelectDateWidget - 如何分别到达每个子字段?

python - 检查时间戳列是否在另一个数据帧的日期范围内

python - 尝试使用 Pandas 和 XLSXwriter 将 CSV 条目添加到电子表格中时出现类型错误

php - 我的 html 页面中的事件元素出现问题

javascript - 为什么 Escape 键在 Ace 编辑器中不起作用?

python - Flask Gunicorn 应用无法让 __name__ 等于 '__main__'

python - 具有多索引的 Pandas 分区 (.div)

html - 在 div 中居中 href 和图像并删除图像边框