vinay
vinay

Reputation: 61

if condition tag is not working properly

In demo.html

<form method="post">
   {{ form1.as_p }}
</form>
<table class="table table-hover" style="width:80%;">
      <tr>
            <th>Test Case</th>
            <th>File Name</th>
            <th>Coverage </th>
        </tr>

        {% for key, value in d.items %}
           <tr>
               <th>{{ key }} </th>
           </tr>

            {% for k,v in value.items%}
                {% if forloop.counter <= count1 %}
                 <tr>
                      <td>   </td>
                      <td>{{ k }}</td>
                      <td>{{ v }}</td>
                </tr>
                {% endif %}
             {% endfor %}
            {% endfor %}


    </table>

In views.py

class home_changetesting(TemplateView):
template_name = 'demo.html'

def get(self, request):

    form1 = SortForm()

    return render(request,self.template_name, {'form1':form1})



def post(self, request):

    form1 = SortForm(request.POST)  
    count1=int

    if form1.is_valid():     
        count1= request.POST.get('sort')
        print(count1)
     args ={'form1':form1,'count1':count1}

    return render(request,self.template_name, args)    

In forms.py

class SortForm(forms.Form):

sort = forms.ChoiceField(choices=[(x, x) for x in range(1, 11)], required=False,widget=forms.Select())

if condition works only when I declare as {% if forloop.counter <= 2 %} Instead if I use variable as above mentioned code it does not work. Please to help me out what is the error in above code.

As if use {{ count1 }} the value is printing correctly.

Upvotes: 0

Views: 62

Answers (2)

Vaibhav
Vaibhav

Reputation: 1234

You are probably passing the count1 as string into the template. To compare it, you need to cast it into an int in your view or use the add filter:

{% if forloop.counter <= count1|add:"0" %}

Upvotes: 1

Exprator
Exprator

Reputation: 27523

args ={'form1':form1,'count1':int(count1)}

use this

as you are taking input,those always comes as a string, thus you need to convert it to int before comparing with a number in template

Upvotes: 2

Related Questions