Only last label input value being returned in Django

馋奶兔 提交于 2019-12-24 15:51:57

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/47056480/only-last-label-input-value-being-returned-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!