Django中仅返回最后一个标签输入值 [英] Only last label input value being returned in Django

查看:152
本文介绍了Django中仅返回最后一个标签输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中是个新手,我想有些事情我正在忽略。我有一个可以动态填充的表单,如下所示

I am pretty new in Django and I guess there is something I am overlooking. I have a form that I am populating dynamically as shown below

<form method="post">
                {% csrf_token %}
                {{ profile.days }}//Only prints last radio button value
                {% for period, value in profile.periods.items %}

                <h2>{{ period }} Reports</h2>
                <label>
                    <input name="days" value={{ value }} type="hidden">
                    <input
                            name="reports_allowed"
                            type="radio"
                            {% if profile.reports_allowed and profile.days == value %} checked {% endif %}>
                    Each {{ value }} send me a summary of my checks
                </label>
                {% endfor %}
                <button
                        name="update_reports_allowed"
                        type="submit"
                        class="btn btn-default pull-right">Save</button>

            </form>

我希望能够按如下方式访问所选单选按钮的值

I want to be able to access the value of the selected radio button which I am doing as follows

form = ReportSettingsForm(request.POST)
        if form.is_valid():
            print(form.cleaned_data)
            days = form.cleaned_data["days"]
            print(days)# Prints only the last value always

关于如何获得单选按钮的价值的任何帮助都将得到高度重视。

Any help on how to get value of radio button clicked will be highly appleciated.

推荐答案

您无需从单选按钮中清除数据,因为它是直接填充的,请尝试类似 selected_choice = request.POST ['choice'])之类的方法,希望对您有所帮助

You don't need to clean data from a radio button since it is populated directly, try something like selected_choice = request.POST['choice']) , hope this helps.

这篇关于Django中仅返回最后一个标签输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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