Django 如果已经检测到值,如何控制for循环?

Django 如果已经检测到值,如何控制for循环?,django,Django,\html \模型 def scheduleofpayment(request): payment = ScheduleOfPayment.objects.all().filter(Education_Levels=paymentsid).order_by('Display_Sequence') return render(request, 'accounts/scheduleofpayment.html', {"payment":payment}) 这是我的管理网站上的图片 这是我在付款计

\html

\模型

def scheduleofpayment(request):
payment = ScheduleOfPayment.objects.all().filter(Education_Levels=paymentsid).order_by('Display_Sequence')
return render(request, 'accounts/scheduleofpayment.html', {"payment":payment})
这是我的管理网站上的图片

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


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

如果你的目标只是在支付类型下拥有
现金
每月
,那么你可以使用QuerySet的
distinct()
方法

views.py

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

views.py

html


\html
--支付类型--
{付款类型中的类型为%u%}
{{type.name}}
{%endfor%}
{%endfor%}

控制循环是什么意思?目标是在下拉列表中只显示两个选项(现金和每月)?是的,正如您在下拉列表中看到的,太多的“每月”是吗?我只希望下拉列表中的结果是“现金”和“每月”,谢谢paolo爵士的努力,但是distinct()方法不起作用,我不知道为什么,顺便说一句,关于模型PaymentType,我有一个特别的原因不使用它。我使用postgressqlpayment=ScheduleOfPayment.objects.all().filter(Education_Levels=paymentsid)。按('Payment_Type')。distinct('Payment_Type')我这样做了,保罗爵士,我得到了我想要的答案,即使你的答案不正确,你给我一个想法来纠正我的问题,谢谢先生,我会将你的答案标记为正确:)哦,抱歉,我错过了对象名称。是的,您是正确的,它是
.distinct('Payment\u Type')
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)
  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})
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
<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>