Итерация нескольких циклов for на основе одного и того же списка - PullRequest
0 голосов
/ 03 октября 2018

В моем шаблоне django мне нужно добавить две петли на основе одного и того же сжатого списка.К сожалению, кажется, что выполняется только первый цикл.

У меня есть следующий код в views.py:

@login_required
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)
    formslot = SlotForm()
    formspecialdays = SpecialdaysForm()
    formweekdays = WeekdaysForm()
    weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
    weekdaynum = [0,1,2,3,4,5,6]
    weekzip = zip(weekdays, weekdaynum)
    newweekzip = weekzip
    return render(request, 'clinic/editslots.html', {'rnd_num': randomnumber(), 'clinic': cl, 'doctor': doc, 'formslot': formslot, 'formspecialdays': formspecialdays, 'formweekdays': formweekdays, 'weekzip': weekzip, 'newweekzip': newweekzip })

Мой шаблон:

<div class="container ml-5 mr-5">
    <div class="jumbotron slotgroup slotavailable mb-1 mt-5" id="jumbo_week_avail">
      <div class="slot-header" role="alert">
        Enter your weekly consultation hours at {{ clinic.name }}. This will supercede regular hours. If you specify some week days, but not others, you will be assumed to be on leave during those days. If you dont specify any week days, but specify regular hours, you will be assumed to be working on all days.
      </div>
      {% for weekday, weeknum in weekzip %}
      <div class="row row_week_avail" id="row_week_avail{{ weeknum }}">
        <div class="col-md-1 mr-2">
          <label class="switch switch_type1 greenswitch" role="switch">
          <input type="checkbox" id="chk_week_avail{{ weeknum }}" class="switch__toggle">
          <span class="switch__label"></span>
        </label>
        </div>
        <div class="col-md-2 text-right">
          <span class="">{{ weekday }}</span>
        </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="start_week_avail{{ weeknum }}" aria-describedby="mainslotstarthelp" placeholder="Starts at">
            <small id="mainslotstarthelp" class="form-text text-muted">Start time of consultations</small>
          </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="end_week_avail{{ weeknum }}" aria-describedby="mainslotendhelp" placeholder="Ends at">
            <small id="mainslotendhelp" class="form-text text-muted">End time of consultations</small>
          </div>
          <div class="col-md-6">
              <a class="btn btn-primary AddHoursBtn" id="btn_week_avail{{ weeknum }}"><i class="fas fa-plus"></i></a>
          </div>
      </div>
      {% endfor %}
    </div>

    <div class="jumbotron slotgroup slotunavailable mb-1" id="jumbo_week_break">
      <div class="slot-header" role="alert">
        Break Time (Unavailable Hours) <span class="text-muted">Time in between regular period, where you are unavailable.</span>
      </div>
      {% for weekday, weeknum in weekzip %}
        <div class="row row_week_break" id="row_week_break{{ weeknum }}">
          <div class="col-md-1 mr-2">
            <label class="switch switch_type1 greenswitch" role="switch">
            <input type="checkbox" id="chk_week_break{{ weeknum }}" class="switch__toggle">
            <span class="switch__label"></span>
          </label>
          </div>
          <div class="col-md-2 text-right">
            <span class="">{{ weekday }}</span>
          </div>
            <div class="col-md-6">
              <input type="text" class="form-control timeinput" id="start_week_break{{ weeknum }}" aria-describedby="mainslotstarthelp" placeholder="Starts at">
              <small id="mainslotstarthelp" class="form-text text-muted">Start time of consultations</small>
            </div>
            <div class="col-md-6">
              <input type="text" class="form-control timeinput" id="end_week_break{{ weeknum }}" aria-describedby="mainslotendhelp" placeholder="Ends at">
              <small id="mainslotendhelp" class="form-text text-muted">End time of consultations</small>
            </div>
            <div class="col-md-6">
                <a class="btn btn-primary AddHoursBtn" id="btn_week_break{{ weeknum }}"><i class="fas fa-plus"></i></a>
            </div>
        </div>
      {% endfor %}
    </div>
</div>

Во время рендерингаВторой цикл for не повторяется.Предоставленный html:

<div class="container ml-5 mr-5">
    <div class="jumbotron slotgroup slotavailable mb-1 mt-5" id="jumbo_week_avail">
      <div class="slot-header" role="alert">
        Enter your weekly consultation hours at Dr Joel's ENT Clinic. This will supercede regular hours. If you specify some week days, but not others, you will be assumed to be on leave during those days. If you dont specify any week days, but specify regular hours, you will be assumed to be working on all days.
      </div>

      <div class="row row_week_avail" id="row_week_avail0">
        <div class="col-md-1 mr-2">
          <label class="switch switch_type1 greenswitch" role="switch">
          <input type="checkbox" id="chk_week_avail0" class="switch__toggle">
          <span class="switch__label"></span>
        </label>
        </div>
        <div class="col-md-2 text-right">
          <span class="">Sunday</span>
        </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="start_week_avail0" aria-describedby="mainslotstarthelp" placeholder="Starts at">
            <small id="mainslotstarthelp" class="form-text text-muted">Start time of consultations</small>
          </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="end_week_avail0" aria-describedby="mainslotendhelp" placeholder="Ends at">
            <small id="mainslotendhelp" class="form-text text-muted">End time of consultations</small>
          </div>
          <div class="col-md-6">
              <a class="btn btn-primary AddHoursBtn" id="btn_week_avail0"><i class="fas fa-plus"></i></a>
          </div>
      </div>

      <div class="row row_week_avail" id="row_week_avail1">
        <div class="col-md-1 mr-2">
          <label class="switch switch_type1 greenswitch" role="switch">
          <input type="checkbox" id="chk_week_avail1" class="switch__toggle">
          <span class="switch__label"></span>
        </label>
        </div>
        <div class="col-md-2 text-right">
          <span class="">Monday</span>
        </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="start_week_avail1" aria-describedby="mainslotstarthelp" placeholder="Starts at">
            <small id="mainslotstarthelp" class="form-text text-muted">Start time of consultations</small>
          </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="end_week_avail1" aria-describedby="mainslotendhelp" placeholder="Ends at">
            <small id="mainslotendhelp" class="form-text text-muted">End time of consultations</small>
          </div>
          <div class="col-md-6">
              <a class="btn btn-primary AddHoursBtn" id="btn_week_avail1"><i class="fas fa-plus"></i></a>
          </div>
      </div>

      <div class="row row_week_avail" id="row_week_avail2">
        <div class="col-md-1 mr-2">
          <label class="switch switch_type1 greenswitch" role="switch">
          <input type="checkbox" id="chk_week_avail2" class="switch__toggle">
          <span class="switch__label"></span>
        </label>
        </div>
        <div class="col-md-2 text-right">
          <span class="">Tuesday</span>
        </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="start_week_avail2" aria-describedby="mainslotstarthelp" placeholder="Starts at">
            <small id="mainslotstarthelp" class="form-text text-muted">Start time of consultations</small>
          </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="end_week_avail2" aria-describedby="mainslotendhelp" placeholder="Ends at">
            <small id="mainslotendhelp" class="form-text text-muted">End time of consultations</small>
          </div>
          <div class="col-md-6">
              <a class="btn btn-primary AddHoursBtn" id="btn_week_avail2"><i class="fas fa-plus"></i></a>
          </div>
      </div>

      <div class="row row_week_avail" id="row_week_avail3">
        <div class="col-md-1 mr-2">
          <label class="switch switch_type1 greenswitch" role="switch">
          <input type="checkbox" id="chk_week_avail3" class="switch__toggle">
          <span class="switch__label"></span>
        </label>
        </div>
        <div class="col-md-2 text-right">
          <span class="">Wednesday</span>
        </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="start_week_avail3" aria-describedby="mainslotstarthelp" placeholder="Starts at">
            <small id="mainslotstarthelp" class="form-text text-muted">Start time of consultations</small>
          </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="end_week_avail3" aria-describedby="mainslotendhelp" placeholder="Ends at">
            <small id="mainslotendhelp" class="form-text text-muted">End time of consultations</small>
          </div>
          <div class="col-md-6">
              <a class="btn btn-primary AddHoursBtn" id="btn_week_avail3"><i class="fas fa-plus"></i></a>
          </div>
      </div>

      <div class="row row_week_avail" id="row_week_avail4">
        <div class="col-md-1 mr-2">
          <label class="switch switch_type1 greenswitch" role="switch">
          <input type="checkbox" id="chk_week_avail4" class="switch__toggle">
          <span class="switch__label"></span>
        </label>
        </div>
        <div class="col-md-2 text-right">
          <span class="">Thursday</span>
        </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="start_week_avail4" aria-describedby="mainslotstarthelp" placeholder="Starts at">
            <small id="mainslotstarthelp" class="form-text text-muted">Start time of consultations</small>
          </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="end_week_avail4" aria-describedby="mainslotendhelp" placeholder="Ends at">
            <small id="mainslotendhelp" class="form-text text-muted">End time of consultations</small>
          </div>
          <div class="col-md-6">
              <a class="btn btn-primary AddHoursBtn" id="btn_week_avail4"><i class="fas fa-plus"></i></a>
          </div>
      </div>

      <div class="row row_week_avail" id="row_week_avail5">
        <div class="col-md-1 mr-2">
          <label class="switch switch_type1 greenswitch" role="switch">
          <input type="checkbox" id="chk_week_avail5" class="switch__toggle">
          <span class="switch__label"></span>
        </label>
        </div>
        <div class="col-md-2 text-right">
          <span class="">Friday</span>
        </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="start_week_avail5" aria-describedby="mainslotstarthelp" placeholder="Starts at">
            <small id="mainslotstarthelp" class="form-text text-muted">Start time of consultations</small>
          </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="end_week_avail5" aria-describedby="mainslotendhelp" placeholder="Ends at">
            <small id="mainslotendhelp" class="form-text text-muted">End time of consultations</small>
          </div>
          <div class="col-md-6">
              <a class="btn btn-primary AddHoursBtn" id="btn_week_avail5"><i class="fas fa-plus"></i></a>
          </div>
      </div>

      <div class="row row_week_avail" id="row_week_avail6">
        <div class="col-md-1 mr-2">
          <label class="switch switch_type1 greenswitch" role="switch">
          <input type="checkbox" id="chk_week_avail6" class="switch__toggle">
          <span class="switch__label"></span>
        </label>
        </div>
        <div class="col-md-2 text-right">
          <span class="">Saturday</span>
        </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="start_week_avail6" aria-describedby="mainslotstarthelp" placeholder="Starts at">
            <small id="mainslotstarthelp" class="form-text text-muted">Start time of consultations</small>
          </div>
          <div class="col-md-6">
            <input type="text" class="form-control timeinput" id="end_week_avail6" aria-describedby="mainslotendhelp" placeholder="Ends at">
            <small id="mainslotendhelp" class="form-text text-muted">End time of consultations</small>
          </div>
          <div class="col-md-6">
              <a class="btn btn-primary AddHoursBtn" id="btn_week_avail6"><i class="fas fa-plus"></i></a>
          </div>
      </div>

    </div>

    <div class="jumbotron slotgroup slotunavailable mb-1" id="jumbo_week_break">
      <div class="slot-header" role="alert">
        Break Time (Unavailable Hours) <span class="text-muted">Time in between regular period, where you are unavailable.</span>
      </div>

    </div>
</div>

Screenshot

Почему это происходит?

1 Ответ

0 голосов
/ 03 октября 2018

В Python 3 zip возвращает генератор: после итерации он исчерпан.Сначала вы должны преобразовать его в список:

weekzip = list(zip(weekdays, weekdaynum))
...