bas*_*sh- 91 django django-templates
我在db中的isoformat中有日期,%Y-%m-%d但是当日期传递给模板时,它就像是一样Oct. 16, 2011.
无论如何,我可以操纵格式到我想要的任何东西?
med*_*nds 244
在模板中,您可以使用Django的date过滤器.例如:
<p>Birthday: {{ birthday|date:"M d, Y" }}</p>
Run Code Online (Sandbox Code Playgroud)
得到:
生日:1983年1月29日
日期过滤器文档中的更多格式示例.
Cir*_*四事件 25
设置两个DATE_FORMAT和USE_L10N
要在Django 1.4.1中对整个站点进行更改,请添加:
DATE_FORMAT = "Y-m-d"
Run Code Online (Sandbox Code Playgroud)
到您的settings.py文件并编辑:
USE_L10N = False
Run Code Online (Sandbox Code Playgroud)
因为l10n覆盖了 DATE_FORMAT
记录在以下网址:https://docs.djangoproject.com/en/dev/ref/settings/#date-format
小智 15
只是用这个
{{you_date_field|date:'Y-m-d'}}
Run Code Online (Sandbox Code Playgroud)
这将显示类似于2016-10-16你可以使用你想要的格式.
小智 7
您需要的是日期模板过滤器。
例如:
<td>Joined {{user.date_created|date:"F Y" }}<td>
Run Code Online (Sandbox Code Playgroud)
这返回 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) )
Run Code Online (Sandbox Code Playgroud)
如果您需要显示较短的日期和时间(11/08/2018 03:23 am),您可以这样做:
{{your_date_field|date:"SHORT_DATE_FORMAT"}} {{your_date_field|time:"h:i a"}}
Run Code Online (Sandbox Code Playgroud)
此标签的详细信息在此处,有关根据给定格式的日期的更多信息在此处。
例子:
<small class="text-muted">Last updated: {{your_date_field|date:"SHORT_DATE_FORMAT"}} {{your_date_field|time:"h:i a"}}</small>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
164873 次 |
| 最近记录: |