Django:在settings.py中使用DATE_FORMAT,DATETIME_FORMAT,TIME_FORMAT? [英] Django: Use of DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT in settings.py?

查看:190
本文介绍了Django:在settings.py中使用DATE_FORMAT,DATETIME_FORMAT,TIME_FORMAT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在全球(通过我的整个网站,管理员和前端)调整日期和时间显示给我的喜好的方式,但我无法弄清楚DATE_FORMAT,DATETIME_FORMAT和TIME_FORMAT变量发生了什么settings.py。



这个问题它表示设置被忽略。问题是一年以上。在Django文档中,它表示当您有 USE_L10N = True 并且显然改变了某些内容。根据这个可能会有一个错误。



我目前使用的是Django 1.2,当我有 USE_L10N = True 它只是忽略settings.py中的日期(时间)格式。当我有 USE_L10N = False 它也似乎忽略它们。



有没有办法全局自定义日期和时间显示?或者我应该在Karen在Django Users Google Group的帖子中建立自己的自定义格式文件?

解决方案

有同样的问题,解决方案简单记录的。每当你呈现一个日期时,你需要指定你想要将模板渲染为日期/时间/短日期/日期时间(例如 {{some_date_var | date}} 和那么它将在您的settings.py



中的 DATE_FORMAT 中指定示例:

 >>>从django.conf导入设置#导入以在settings.py 
>>>中显示我的变量settings.DATE_FORMAT# - 显示我的值;我修改了这个值
'm / d / Y'
>>> settings.TIME_FORMAT
'P'
> >> settings.DATETIME_FORMAT
'N j,Y,P'
>> from django.template import Template,Context
>> from datetime import datetime
>>> c = Context(dict(moon = datetime(1969,7,20,20,17,39)))#创建使用datetime渲染模板的上下文
> ;>> print c ['moon']#这是打印datetime对象的默认格式
1969-07-20 20:17:39
> ;>>打印模板(默认格式:{{moon}} \\\

使用DATE_FORMAT:{{moon | date}} \\\

使用TIME_FORMAT:{{月亮|时间}} \\\

使用DATETIME_FORMAT:{{moon | date:'DATETIME_FORMAT'}} \\\

使用SHORT_DATETIME_FORMAT:{{moon | date:'SHORT_DATETIME_FORMAT'} }
).render(c)
默认格式:1969-07-20 20:17:39
使用DATE_FORMAT:07/20/1969
使用TIME_FORMAT:8: 17 pm
使用DATETIME_FORMAT:1969年7月20日,8:17 pm
使用SHORT_DATETIME_FORMAT:07/20/1969 8:17 pm

这是有道理的;例如,模板需要知道是否应该使用 DATE_FORMAT SHORT_DATE_FORMAT 或其他任何内容。


I would like to globally (through my entire site, admin and front-end) adjust the way dates and time are displayed to my likings, but I cannot figure out what is going on with the DATE_FORMAT, DATETIME_FORMAT and TIME_FORMAT variables in settings.py.

In this question it says that the settings are ignored. The question is over a year old though. In the Django documentation it says they can be used when you have USE_L10N = True and apparently something changed in Django 1.2. According to this however there might be a bug.

I am currently using Django 1.2 and when I have USE_L10N = True it just ignores the date(time) format in settings.py. When I have USE_L10N = False it also seems to ignore them.

Is there a way to globally customize the date and time display? Or should I create my own custom formats file as Karen suggests in the Django Users Google Group post?

解决方案

Had same problem, solution is simple and documented. Whenever you render a date, you need to specify you want the template to render it as a date/time/short_date/datetime (e.g., {{ some_date_var | date }} and then it will render it as specified with DATE_FORMAT in your settings.py

Example:

>>> from django.conf import settings  # imported to show my variables in settings.py 
>>> settings.DATE_FORMAT #  - showing my values; I modified this value
'm/d/Y'
>>> settings.TIME_FORMAT
'P'
>>> settings.DATETIME_FORMAT
'N j, Y, P'
>>> from django.template import Template, Context
>>> from datetime import datetime
>>> c = Context(dict(moon = datetime(1969, 7, 20, 20, 17, 39))) # Create context with datetime to render in a template
>>> print c['moon'] # This is the default format of a printing datetime object 
1969-07-20 20:17:39
>>> print Template("default formatting : {{ moon }}\n"
                   "use DATE_FORMAT : {{ moon|date }}\n"
                   "use TIME_FORMAT : {{ moon|time }}\n"
                   "use DATETIME_FORMAT: {{ moon|date:'DATETIME_FORMAT' }}\n"
                   "use SHORT_DATETIME_FORMAT: {{ moon|date:'SHORT_DATETIME_FORMAT' }}"
                   ).render(c)
default formatting : 1969-07-20 20:17:39
use DATE_FORMAT : 07/20/1969
use TIME_FORMAT : 8:17 p.m.
use DATETIME_FORMAT: July 20, 1969, 8:17 p.m.
use SHORT_DATETIME_FORMAT: 07/20/1969 8:17 p.m.

This makes sense; e.g., the template needs to know whether it should use the DATE_FORMAT or the SHORT_DATE_FORMAT or whatever.

这篇关于Django:在settings.py中使用DATE_FORMAT,DATETIME_FORMAT,TIME_FORMAT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆