2

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..

3
  • Please do not indent your text paragraphs
    – Marcin
    Feb 9, 2012 at 11:00
  • are you looking for {% if form.topics %} construct?
    – akonsu
    Feb 9, 2012 at 12:17
  • Well i am just looking for calculating the length of form.topics or len(f.fields['topics'].queryset)
    – the_game
    Feb 10, 2012 at 6:23

1 Answer 1

4

I would suggest you to calculate the length of the variable

forms.topics

in your view and simply use this variable in your template as

{% if not forms.topic or variable <= 1 %}
    <td>Whatever you want to display</td>
{% else %}
    <td> {{ forms.topic }} </td>
{% endif %}

This code checks if there is no value in "forms.topic" or the variable's length(Which you calculated in your view) is less than or equal to 1. Print the text you want to display.

2
  • Thats exactly the problem is i am using variable = len( f.fields['topics'].queryset) also i have used count and it is showing manager has nor attribute len().After calculating the length of that field i can use it in templates.How do i calculate the length ?What mistake i am doing ?
    – the_game
    Feb 10, 2012 at 6:26
  • @the_game: I think you should firstly open the python shell and check out for what f.fields['topics'].queryset is yielding, is it able to fetch certain value for you, may it be a tuple, list or string. Please confirm that first.
    – Prateek
    Feb 14, 2012 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.