首页 > 解决方案 > 如果已经检测到值,如何控制 for 循环?

问题描述

\html

<select id="payments" name ="payments" onchange="payment(this.value)">
    <option value="0" name ="yearlvllist">-- Payment Type --</option>
   {% for paymentschedule in payment %}
    <option value="{{paymentschedule.Payment_Type.id}}" name ="payments">{{paymentschedule.Payment_Type}}</option>
    {% endfor%}
</select>

\视图

def scheduleofpayment(request):
payment = ScheduleOfPayment.objects.all().filter(Education_Levels=paymentsid).order_by('Display_Sequence')
return render(request, 'accounts/scheduleofpayment.html', {"payment":payment})

\楷模

class ScheduleOfPayment(models.Model):
    Pending_Request = [
       ('Active', 'Active'),
       ('Inactive', 'Inactive'),
    ]
    Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE, blank=True, null=True)
    Courses = models.ForeignKey(Course, related_name='+', on_delete=models.CASCADE,blank=True, null=True)
    Payment_Type = models.ForeignKey(PaymentType, related_name='+', on_delete=models.CASCADE, blank=True, null=True)
    Display_Sequence = models.IntegerField(blank=True, null=True)
    Date = models.DateField(null=True,blank=True)
    Amount = models.FloatField(null=True, blank=True)
    Remark = models.CharField(max_length=500,blank=True, null=True)
    Status = models.CharField(max_length=500, null=True, choices=Pending_Request,blank=True)

    def __str__(self):
        suser = '{0.Education_Levels}'
        return suser.format(self)

这是来自我的管理站点 管理站点的图像

这是我在付款 用户计划中过滤的结果

我的问题是如何控制html模板中每月的循环?不删除数据库。你们有什么想法吗?

标签: django

解决方案


如果您的目标只是拥有CashMonthly付款类型下,那么您可以使用 QuerySet 的distinct()方法。

视图.py

  def scheduleofpayment(request):
      payment = ScheduleOfPayment.objects.all().filter(Education_Levels=paymentsid)
                .order_by('Display_Sequence').distinct('Payment_Type') 
return render(request, 'accounts/scheduleofpayment.html', {"payment":payment})

或者

既然你有一个支付类型的模型,你不能用它来显示你的选项吗?

视图.py

def scheduleofpayment(request):
      payment = ScheduleOfPayment.objects.all().filter(Education_Levels=paymentsid)
                .order_by('Display_Sequence')
      payment_type = PaymentType.objects.all()

      context = {
          'payment': payment,
          'payment_type': payment_type,
      }

return render(request, 'accounts/scheduleofpayment.html', context)

html



\html
<select id="payments" name ="payments" onchange="payment(this.value)">
    <option value="0" name ="yearlvllist">-- Payment Type --</option>
    {% for type in payment_type %}
        <option value="{{type.id}}" name ="payments"> 
        {{ type.name }}</option>
    {% endfor%}
    {% endfor%}
</select>

推荐阅读