使用日期过滤器手动渲染django模板

时间:2010-11-30 20:06:05

标签: django django-templates

我手动渲染模板,但没有过滤器的日期。有没有一种方法可以在我将它们放入上下文之前格式化日期?

以下是代码:

t= This is to notify you that you are booked on the course {{title}} that starts on {{start_date|date:"F j, Y"}} at {{start|time}} and is being held at {{venue}}.

c = {'startdate': datetime.datetime(2010, 12, 1, 9, 0), 'enddate': datetime.datetime(2010, 12, 1, 12, 0), 'has_cpd': False, 'title': 'Course 1', 'closes': None, 'creator': 1L, 'venue': 'Venue for Course 1', 'summary': 'Course 1 Summary', 'tags': '', 'attachment': <FieldFile: None>, 'organiser': 3L, 'id': 1L, 'opens': datetime.datetime(2010, 11, 30, 20, 1, 50, 951490), 'venue_map': None}

t.render(c)

这给出了输出:

This is to notify you that you are booked on the course Course 1 that starts on  at  and is being held at Venue for Course 1.

为什么日期不显示?

1 个答案:

答案 0 :(得分:1)

看起来像是start_date与startdate之间的简单拼写错误。

In [18]: t= Template("""This is to notify you that you are booked on the course {{title}} that starts on {{startdate|date:"F j, Y"}} at {{start|time}} and is being held at {{venue}}.""")
In [19]: t.render(c)Out[19]: u'This is to notify you that you are booked on the course Course 1 that starts on December 1, 2010 at  and is being held at Venue for Course 1.'