About the problem :- I have a form field named topics which is manytomanyfield. Now in the template i am calling like {{form.topics}}
in a div inside a fieldset.I want to check if the {{form.topics}}
is empty or its length is <=1 in that case I don't want to display the fieldset of {{form.topics}}
Here is my code.I am solving this problem using jquery.
forms.py
# Showing only that field to keep code short
class VisitSetupForm(Form):
topics = ModelMultipleChoiceField(
queryset=Topic.objects.filter(reporting=False),
widget=CheckboxSelectMultiple,
required=False
)
Views.py
def setup(request):
if request.user.is_superuser:
form_class = AdminVisitSetupForm
all_topics = True
else:
form_class = VisitSetupForm
all_topics = False
f = form_class(request, data=request.POST or None)
if request.method == "POST":
if f.is_valid():
......so on ....
if request.user.is_superuser:
topics = cd['topics']
else:
topics = set(list(interview.topics.all()) + list(cd['topics']))
next_url = "/visit/confirmation/%s/%s/?next=%s" % (patient.user.id, interview.id, url)
return HttpResponseRedirect(next_url)
if not all_topics:
user = get_user(request)
# checking here if the topics exists for other user
f.fields['topics'].queryset = user.organization.topics
f.fields['interview'].queryset = user.organization.interviews
data['form'] = f
return render_to_response('visit/setup.html', data, context_instance=RequestContext(request))
.html
# calling in html
<fieldset class="step4">
<legend>Step 4 - Topic selection</legend>
<p>Check off any additional topics you want to add to the interview. If you want to
remove a topic from an interview, uncheck it.</p>
<div>{{ form.topics }}</div>
</fieldset>
<script>
if($(".step4 input:checkbox").length <= 0)
{
$(".step4").hide();
}
</script>
{{form.topics}} is list of checkboxes.I want when there is no checkboxes ({{form.topics is empty}}) do not display the fieldset This is achieved through jquery.I want something like {{form.topics.empty}} do not display the step4 fieldset. Is there any good way so that i can remove that jquery.
Thanks in advance..