Pardon a newbie.
I'm building a form that will display workshop selections a person has
made. They'll be able to update their selections using this form.
Selections are represented by radio buttons; I'm trying to make a
radio button the "checked" option as I run through a loop, and nothing
I try seems to work. I'm using {% ifequal %} primarily:
TEMPLATE:
---------------
...
{% for object in workshops %}
<input type="radio" ... value="{{ object.id }}" ... {% ifequal
object.id selections.groupA %} checked {% endifequal %} />
...
{% endfor %}
...
VIEW:
---------
...
workshops = [a queryset]
selections = [a different queryset]
return render_to_response('workshops/form.html',
{
...
'selections': selections,
'workshops': workshops,
...
},
context_instance=RequestContext(request))
I can't seem to get the value of selections.groupA while inside the
for loop; I can get it just fine anywhere else in the template.
I may be barking up the wrong tree; I've scoured the Internet and the
documentation to no avail.
I would appreciate it if someone can point out what I'm missing and/or
suggest an alternative.
TIA
Assuming that 'groupA' is a model field, there's no such property as
'selections.groupA', because as you've stated, selections is a
queryset - ie a collection of model instances, not a single one.
However in general you'd be better off using Django's forms framework,
which outputs fields for you with all the correct attributes selected.
--
DR.
Co-incidentally I was asking a very similar question, I couldn't see
any way to use the model information in a formset. In the first
instance I'm just passing in the data, I assume it isn't possible to
use that as variables elsewhere?
Also, in the view, is it possible to reference a model associated with
each form in the formset?
This is a simplified version of the view:
http://dpaste.com/172317/
That's aimed at taking in multiple events and multiple members, which
is fine except that I can't see how to reference the members when:
- In the form (template).
- Iterating over the form information.
E.g. this doesn't work:
for entry_form in entry_formset.cleaned_data:
this_member = members[entry_form]
Any pointers appreciated, I can't find a similar instance in the docs.
Kind regards,
-Alastair
selections is a single record, obtained this way...
selections =
Workshop.objects.get(user__username=request.user.username)
I have no trouble accessing/printing the value of selections.groupA
(or selections.groupB, etc.) unless I'm inside the "for" loop that
displays the Workshops (I'd like to highlight the workshops that the
user has selected).
selections.groupA is the id for the Workshop they've selected.
The basic Django form didn't give me the interaction I was looking
for. I want people to use radio buttons to select one Workshop from
among a group of five. (That gets submitted as their Group A
selection).
1) Discovered that I was not in fact comparing like data types, so {%
ifequal %} was bound to fail. (Pardon my newb-ness)
2) Once I fixed that problem, I ended up putting six ifequals together
in the loop, so that the radio button would be checked if any one of
the six possible selections in the "selections" list was equal:
...
<input type="radio" name="group{{ object.group_code|upper }}"
id="id_group{{ object.group_code|upper }}" value="{{ object.id }}"
validate="required:true"
{% ifequal object.id selections.groupA %} checked {% endifequal
%}
{% ifequal object.id selections.groupB %} checked {% endifequal
%}
{% ifequal object.id selections.groupC %} checked {% endifequal
%}
{% ifequal object.id selections.groupD %} checked {% endifequal
%}
{% ifequal object.id selections.groupE %} checked {% endifequal
%}
{% ifequal object.id selections.groupF %} checked {% endifequal
%} />
...
Don't see why you can't do that with Django forms.
class MyForm(forms.Form):
group = forms.ModelChoiceField(queryset=Group.objects.all(),
empty_label=None, widget=forms.RadioSelect)
--
DR.