Django mark / select 列表视图中的一些数据
Django mark / select some data in listview
我有这样的列表视图 class :
class VechileListView(ListView):
model = Vechile
template_name = 'store/arm_list.html'
context_object_name = 'arm'
在模型Vechile
中,有字段tanggal_stnk
它工作正常,现在的问题是我想在 tanggal_stnk
中将一些数据标记为红色
这是模板:
{% if arm %}
{% for data in arm %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ data.tanggal_stnk }}</td>
.......
tanggal_stnk
是 datefield
,例如我想将文本标记为红色,如果日期是今天..
我已经尝试在 Class VechileListView
中添加类似的内容
class VechileListView(ListView):
model = Vechile
template_name = 'store/arm_list.html'
context_object_name = 'arm'
def expiry(request):
expired_stnk = Vechile.objects.filter(tanggal_stnk = datetime.now())
context = {
'expired_stnk' : expired_stnk
}
return render(request, 'store/arm_list.html', context)
在listView中是否可行,如何在模板中实现,谢谢
您可以使用指定今天是否过期的布尔值来注释查询集:
from django.db.models import BooleanField, ExpressionWrapper, Q
from django.utils.timezone import now
class VechileListView(ListView):
model = Vechile
template_name = 'store/arm_list.html'
context_object_name = 'arm'
def get_queryset(request):
return Vechile.objects.annotate(
expires_today=ExpressionWrapper(
Q(<b>tanggal_stnk=now().date()</b>),
output_field=BooleanField()
)
)
然后我们渲染它:
{% for data in arm %}
<tr>
<td>{{ forloop.counter }}</td>
<td<b>{% if data.expires_today %}</b> style="color:#0000ff"<b>{% endif %}</b>>{{ data.tanggal_stnk }}</td>
</tr>
{% endfor %}
我有这样的列表视图 class :
class VechileListView(ListView):
model = Vechile
template_name = 'store/arm_list.html'
context_object_name = 'arm'
在模型Vechile
中,有字段tanggal_stnk
它工作正常,现在的问题是我想在 tanggal_stnk
中将一些数据标记为红色
这是模板:
{% if arm %}
{% for data in arm %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ data.tanggal_stnk }}</td>
.......
tanggal_stnk
是 datefield
,例如我想将文本标记为红色,如果日期是今天..
我已经尝试在 Class VechileListView
class VechileListView(ListView):
model = Vechile
template_name = 'store/arm_list.html'
context_object_name = 'arm'
def expiry(request):
expired_stnk = Vechile.objects.filter(tanggal_stnk = datetime.now())
context = {
'expired_stnk' : expired_stnk
}
return render(request, 'store/arm_list.html', context)
在listView中是否可行,如何在模板中实现,谢谢
您可以使用指定今天是否过期的布尔值来注释查询集:
from django.db.models import BooleanField, ExpressionWrapper, Q
from django.utils.timezone import now
class VechileListView(ListView):
model = Vechile
template_name = 'store/arm_list.html'
context_object_name = 'arm'
def get_queryset(request):
return Vechile.objects.annotate(
expires_today=ExpressionWrapper(
Q(<b>tanggal_stnk=now().date()</b>),
output_field=BooleanField()
)
)
然后我们渲染它:
{% for data in arm %}
<tr>
<td>{{ forloop.counter }}</td>
<td<b>{% if data.expires_today %}</b> style="color:#0000ff"<b>{% endif %}</b>>{{ data.tanggal_stnk }}</td>
</tr>
{% endfor %}