3

I need to do a counter increment within a loop. I had a look at django for.counter, but unfortunately, my increments dont exactly occur within each iteration of the loop. So is there any way at all that I can implement increment of a variable within django template, without going to great pains to implement a new object in my code to do this without such an increment?

In the following code, I am writing the lines {{ count = 0 }}, {{ count += 1 }} just for illustration purpose. I know it wont work. The following is a very simplified form of my template:

<div class="jumbotron slotgroup slotavailable mb-1 mt-5" id="jumbo_week_avail">
  <div class="slot-header" role="alert">
    Headertext
  </div>
  {% if weeklyslotsav %}
    {% for day,daynum in weekzip %}
    {{ count = 0 }}
      {% if daynum in weeklyslotsav.day %}
        {% for weekslotav in weeklyslotsav %}
          {% if weekslotav.day == daynum %}
          <div class="row row_week_avail{{ weekslotav.day }}" id="row_week_avail{{ weekslotav.day }}_{{ count }}">
          </div>
          {{ count += 1 }}
          {% endif}
        {% endfor %}
      {% else %}
      <div class="row row_week_avail{{ daynum }}" id="row_week_avail{{ daynum }}_0">
      </div>
      {% endif %}
    {% endfor %}
  {% else %}
    {% for weekday, weeknum in weekzip %}
    <div class="row row_week_avail{{ weeknum }}" id="row_week_avail{{ weeknum }}_0">
    </div>
    {% endfor %}
  {% endif %}
</div>

The following is a segment from my views:

def edit_doctorslots(request, cliniclabel, doctor_id):
    doctor_id=int(doctor_id)
    doc = get_object_or_404(doctor, docid=doctor_id)
    cl = Clinic.objects.get(label=cliniclabel)
    print("Clinic name", cl.name)
    regularslotsav = ''
    try:
        regularslotsav = Timeslots.objects.filter(clinic =cl, doctor =doc, available =True)
    except:
        pass
    regularslotsbr = ''
    try:
        regularslotsbr = Timeslots.objects.filter(clinic =cl, doctor =doc, available =False)
    except:
        pass

    weekavzip = ''
    try:
        weeklyslotsav = Weekdays.objects.filter(clinic =cl, doctor =doc, available =True)
        weekav = range(0, len(weeklyslotsav))
        weekavzip = list(zip(weeklyslotsav, weekav))
    except:
        pass
    weeklyslotsbr = ''
    try:
        weeklyslotsbr = Weekdays.objects.filter(clinic =cl, doctor =doc, available =False)
    except:
        pass

    formslot = SlotForm()
    formspecialdays = SpecialdaysForm()
    formweekdays = WeekdaysForm()
    weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
    weekdaynum = [0,1,2,3,4,5,6]
    weekzip = list(zip(weekdays, weekdaynum))
    newweekzip = weekzip

    return render(request, 'clinic/editslots0.html', {'rnd_num': randomnumber(), 'clinic': cl, 'doctor': doc, 'formslot': formslot, 'formspecialdays': formspecialdays, 'formweekdays': formweekdays, 'weekzip': weekzip, 'newweekzip': newweekzip, 'regav': regularslotsav, 'regbr': regularslotsbr, 'weekav': weekavzip, 'weekbr': weeklyslotsbr, 'weeklyslotsav': weeklyslotsav })

I've seen many similiar questions on SO. However in all of them I've seen people introducing for.counter. But this is not suitable for my purpose.

Joel G Mathew
  • 7,561
  • 15
  • 54
  • 86
  • 1
    Then you will need to "move the logic to the view level". Please do not write *imperative* code in a template. Django templates deliberately made that hard to avoid people writing such statements. You will have to add some logic in the view, that for example for each iteration adds the value of such `counter`. – Willem Van Onsem Oct 07 '18 at 08:49
  • Possible duplicate of [Django Template - Increment the value of a variable](https://stackoverflow.com/questions/8659560/django-template-increment-the-value-of-a-variable) – Johan Oct 07 '18 at 08:52
  • @WillemVanOnsem Then sadly I'll have to add additional redundant rows in sql. – Joel G Mathew Oct 07 '18 at 08:52
  • No @Johan because for.counter doesnt solve my problem – Joel G Mathew Oct 07 '18 at 08:53
  • 2
    @Droidzone: no, just add data to the "objects" the view passes to the template. Like you did with the `zip(..)`. – Willem Van Onsem Oct 07 '18 at 08:53
  • @Droidzone, the `for.counter` in the duplicate is specified as the problem. You have the same problem as the duplicate, "Increase a counter in a loop based on filtered conditions". I flagged as duplicate because the answers are likely to be the same: Do the work in the view because it's not supported in the template. – Johan Oct 07 '18 at 08:59

1 Answers1

1

You can use with tag to set variables in template as -

{% with count=0  %}        
   {{ count}}
    ...do other stuffs 
{% endwith %}

and for maths you could use django math filters like

{{ count|add:"1" }}

You can code you with use of both.

For more about setting variable in django template - How to set a value of a variable inside a template code?

and for using math in django - How to do math in a Django template?

Hope this helps you.

Community
  • 1
  • 1
Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50