在 django 模板中获取选中的单选按钮 [英] Get the selected radio button in django template

查看:19
本文介绍了在 django 模板中获取选中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模板中,我有 for 循环.如您所见,每个单选按钮的 id 由 {{stud.id}} 表示.

In my template I have for loop. As you can see the id for each radiobutton is reprsented by {{stud.id}} .

{{stud.nameAndSurname}} 显示学生的姓名和姓氏(在浏览器中我可以看到相应学生的姓名和姓氏).

{{stud.nameAndSurname}} shows the name and the surname of the student(in browser I can see name and surname of the corresponding student).

<form method="POST" class="post-form">
        {% csrf_token %}
        {% for stud in students %}
            <input type="radio" id="{{ stud.id }}" name="student">{{ stud.nameAndSurname  }}
        {% endfor %}
    <button type="submit" class="save btn btn-default">GO!</button>
</form>

在我的 view.py 中我有:

In my view.py I have:

 class MyClass(View):
     def get(...):
         ...

     def post(self,request, pk):
         myVar = request.POST.get("student")
         return HttpResponse(myVar)

如果我点击提交按钮,我想转到另一个页面,显示所选学生的姓名和姓氏.现在的问题是,它向我显示的不是学生的姓名和姓氏,而是简单地写在on"上

If I click submit button i want to go to another page that shows me name and surname of the selected student. Now the issue is that instead of the student's name and surname it's showing me simply written "on"

推荐答案

单选按钮需要有值.(注意输入结束后的部分是标签,应该包含在 label 元素中.)

Radio buttons need to have a value. (Note that the part after the end of the input is the label, and should be enclosed in a label element.)

<input type="radio" id="student_{{ stud.id }}" name="student" value="{{ stud.id }}">
<label for="student_{{ stud.id }}">{{ student.nameAndSurname }}</label>

这篇关于在 django 模板中获取选中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆