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.

6
  • 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. Commented Oct 7, 2018 at 8:49
  • Possible duplicate of Django Template - Increment the value of a variable Commented Oct 7, 2018 at 8:52
  • @WillemVanOnsem Then sadly I'll have to add additional redundant rows in sql. Commented Oct 7, 2018 at 8:52
  • No @Johan because for.counter doesnt solve my problem Commented Oct 7, 2018 at 8:53
  • 2
    @Droidzone: no, just add data to the "objects" the view passes to the template. Like you did with the zip(..). Commented Oct 7, 2018 at 8:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.