问题
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