我在数据库 %Y-%m-%d
中有 ISO 8601 格式的日期。但是,当日期传递到模板时,它会显示为 Oct. 16, 2011
。
有没有办法可以将格式操纵为我想要的任何东西?
在您的模板中,您可以使用 Django 的 date
过滤器。例如:
<p>Birthday: {{ birthday|date:"M d, Y" }}</p>
给出:
生日:1983年1月29日
date filter docs 中有更多格式示例。
同时设置 DATE_FORMAT
和 USE_L10N
要在 Django 1.4.1 中对整个站点进行更改,请添加:
DATE_FORMAT = "Y-m-d"
到您的 settings.py
文件并编辑:
USE_L10N = False
因为 l10n 覆盖 DATE_FORMAT
这记录在:https://docs.djangoproject.com/en/dev/ref/settings/#date-format
只需使用这个:
{{you_date_field|date:'Y-m-d'}}
这将显示类似 2016-10-16 的内容。您可以根据需要使用格式。
您需要一个 date template filter。
例如:
<td>Joined {{user.date_created|date:"F Y" }}<td>
这将返回 Joined December 2018
。
为了更改 views.py 文件中的日期格式,然后将其分配给模板:
# get the object details
home = Home.objects.get(home_id=homeid)
# get the start date
_startDate = home.home_startdate.strftime('%m/%d/%Y')
# assign it to template
return render_to_response('showme.html', {'home_startdate':_startDate}, context_instance=RequestContext(request) )
{{yourDate|date:'*Prefered Format*'}}
例子:
{{yourDate|date:'F d, Y'}}
对于首选格式:Built-in template tags and filters, date
如果您在 Django 模板中调用 Date 值时遇到错误,请试试这个
{{ViewfieldValue.CreateModelValue|date:"M d, Y" }}
不定期副业成功案例分享