python - Datetime object won't work with weekday() -
class event(models.model): day = models.datefield(blank=true, null=true) period = models.integerfield(choices=period_choices) cart = models.charfield(choices=cart_choice, max_length=4) is_reserved = models.booleanfield(default=false) teacher = models.foreignkey(user, blank=true, null=true) class reservationform(modelform): class meta: model = event fields = ('day', 'teacher', 'is_reserved', 'period', 'cart') widgets = {'teacher': forms.hiddeninput(), 'is_reserved': forms.hiddeninput(), 'period': forms.hiddeninput(), 'cart': forms.hiddeninput(), 'day': forms.hiddeninput()}
i want day of week in order sort events. should able using django's date filter - {% field.initial.day|day:"w" %}
- doesn't return anything.
so wrote template tag:
@register.filter def the_day(value): return value.date().weekday() home.html {{ form.initial.day|the_day }}
nope: 'str' object has no attribute 'date'
alright, let's use dateparse
@register.filter def the_day(value): answer = parse_date(value) return answer.weekday()
nope: exception type: typeerror @ / exception value: expected string or bytes-like object
any ideas look?
you can directly use django's inbuilt date
edit:
i guessing need week day in number,
{{ form.initial.day|date:"w" }}
or if want weekday name template tag
{{ form.initial.day|date:"d" }}
Comments
Post a Comment