Python 基于同一列表迭代多个for循环

Python 基于同一列表迭代多个for循环,python,django,python-3.x,django-templates,Python,Django,Python 3.x,Django Templates,在我的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(lab

在我的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>

请在{{clinic.name}输入每周咨询时间。这将取代正常工作时间。如果您指定了一些工作日,但没有指定其他工作日,则假定您在这些工作日休假。如果您没有指定任何工作日,但指定了正常工作时间,则假定您在所有工作日工作。
{weekday的百分比,weekzip%中的weeknum}
{{工作日}
磋商开始时间
协商结束时间


为什么会发生这种情况?

在Python3中,
zip
返回一个生成器:一旦迭代,它就会耗尽。您应首先将其转换为列表:

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