django - 如果已经检测到值,如何控制 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模板中每月的循环?不删除数据库。你们有什么想法吗?
解决方案
如果您的目标只是拥有Cash
并Monthly
在付款类型下,那么您可以使用 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>
推荐阅读
- python - 如何使用 PySpark 在用于字符串的列中用 NULL 替换整数的任何实例?
- angularjs - 如何在响应状态为“已收到”而不是“已启动”的“$http” api 调用中获得真正的响应?
- r - 按模式选择列并立即替换因子
- android - UnknownPluginException 使用 Google Play 服务和插件 DSL
- macos - 回顾macos限制进程的内存
- svelte - 如何在 Svelte 中初始化数组存储?
- java - 我的 MacOs(Catalina) 中的 java 版本混淆
- r - R - 将 Data.frame 元素转换为单列 tibble()
- php - 在所有页面上显示 Wordpress 标题,而不仅仅是首页
- flutter - 所有屏幕的填充