1π
We did something similar. In our code we used template tags (see https://docs.djangoproject.com/en/1.8/howto/custom-template-tags/. In the template tags file we have: (Note you can remove the part about handling datetimes):
from django import template
from datetime import date, datetime, timedelta
register = template.Library()
@register.filter(expects_localtime=True)
def is_today(value):
if isinstance(value, datetime):
value = value.date()
return value == date.today()
@register.filter(expects_localtime=True)
def is_past(value):
if isinstance(value, datetime):
value = value.date()
return value < date.today()
Then in the template you can do:
{% if this_date_object|is_today %} <span class="active">{{field}}</span> {% endif %}
{% if this_date_object|is_past %} <span class="past"> {{field}}</span> {% endif %}
Small advantages here are 1) you can reuse the date comparison in other places, and 2) your donβt clutter your model with code only relevant to how it is to be displayed.
0π
Havenβt tested it but I believe something like the following should work.
If you use a model method to compare the fields with the current date and return an array you should be able to loop through it in the template, so something like this
class MyModel(models.model):
field1 = models.ForignKey(AnotherModel)
field2 = models.DateField(blank=True, null=True)
field3 = models.DateField(blank=True, null=True)
field4 = models.DateField(blank=True, null=True)
field5 = models.DateField(blank=True, null=True)
field10 = models.DateField(blank=True, null=True)
def is_past:
past = []
for field in fields:
if field.date < datetime.now().date():
past.append(True)
else:
past.append(False)
return past
Template:
{% for field in context.is_past %}
{% if field == True %}
<span class="past">{{ field}}</span>
{% else %}
<span class="active">{{ field}}</span>
{% endif %}
{% endfor %}
That should get you on the right track but may need some editing to work properly for you.
- Braintree payments payment_method_nonce value is null
- Optimizing Django Many-To-Many Query with custom through table
0π
define this method on your model
def is_past(self):
fields = (
self.field1,
self.field2,
self.field3,
self.field4,
self.field5,
self.field10,
)
for field in fields:
if field.date < datetime.now().date():
return True
return False
- Django dynamics ChoiceFields choices from APIs raises ValueError
- Uploading multiple images in Django for a single post
- What is the efficient way to connect models with a one to one relation?
- Django messages framework