如何制作 Django 的“DATETIME_FORMAT” 积极的?
DATETIME_FORMAT
应该放在哪里才能生效 在 Django 管理站点中显示日期时间 (Django 的自动管理界面)?
DATETIME_FORMAT
的文档,第 页 http://docs.djangoproject.com/en/1.0/ref/settings/< /a>,说:
"The default formatting to use for datetime fields on
Django admin change-list pages -- and, possibly, by
other parts of the system."
更新 1:DATETIME_FORMAT
已损坏(其值为 忽略),尽管有文档。 许多年前的它 有效,但从那时起 Django 实现就已经 破碎地区 此功能。 好像是 Django 社区 无法决定如何解决它(但与此同时我认为他们 应从文档中删除 DATETIME_FORMAT
或添加 关于这个问题的注释)。
我已将这些行放入文件“settings.py”中 网站/项目(不是应用程序),但似乎没有 有什么影响(重启开发服务器后):
DATETIME_FORMAT = 'Ymd H:i:sO'
DATE_FORMAT = '日期'
例如,当以下情况时显示“June 29, 2009, 7:30 pm” 使用 Django 管理站点。
Django 版本是 1.0.2 最终版本,Python 版本是 2.6.2 (64 位)。 平台:Windows XP 64 位。
Stack Overflow 问题Django Admin 中的欧洲日期输入 似乎是完全相反的问题(因此明显的 矛盾)。
文件“settings.py”的完整路径是 “D:\dproj\MSQall\website\GoogleCodeHost\settings.py”。 我现在 以这种方式启动开发服务器(在 Windows 命令中 线窗口):
cd D:\dproj\MSQall\website\GoogleCodeHost
设置 DJANGO_SETTINGS_MODULE=GoogleCodeHost.settings
python管理.py runserver 6800
没有区别。 除了这些都是积极阅读的 来自文件“settings.py”:
DATABASE_NAME
已安装_APPS
TEMPLATE_DIRS
MIDDLEWARE_CLASSES
“django-admin.py startproject XYZ”不创建文件 “settings.py”包含 DATETIME_FORMAT
或 DATE_FORMAT
。 也许这是有原因的?
序列“d:”,“cd D:\dproj\MSQall\website\GoogleCodeHost”, “python管理.py shell", "从 django.conf 导入设置", “settings.DATE_FORMAT”、“settings.DATETIME_FORMAT”输出 (如预期):
'Y-m-d H:i:sO'
'Y-m-d'
因此正在读取文件“settings.py”的内容,但确实 在 Django 管理界面中不生效。
Where should DATETIME_FORMAT
be placed for it to have effect
on the display of date-time in the Django admin site
(Django’s automatic admin interface)?
Documentation for DATETIME_FORMAT
, on page
http://docs.djangoproject.com/en/1.0/ref/settings/, says:
"The default formatting to use for datetime fields on
Django admin change-list pages -- and, possibly, by
other parts of the system."
Update 1: DATETIME_FORMAT
is broken (the value of it is
ignored), despite the documentation. Many years ago it
worked, but since then the Django implementations have been
broken wrt. this feature. It seems the Django community
can't decide how to fix it (but in the meantime I think they
should remove DATETIME_FORMAT
from the documentation or add
a note about this problem to it).
I have put these lines into file "settings.py" of the
website/project (not the app), but it does not seem to have
any effect (after restarting the development server):
DATETIME_FORMAT = 'Y-m-d H:i:sO'
DATE_FORMAT = 'Y-m-d'
As an example "June 29, 2009, 7:30 p.m." is displayed when
using Django admin site.
Django version is 1.0.2 final and Python version is 2.6.2
(64 bit). Platform: Windows XP 64 bit.
Stack Overflow question European date input in Django Admin seems to be about the exact opposite problem (and thus an apparent
contradiction).
The full path to file "settings.py" is
"D:\dproj\MSQall\website\GoogleCodeHost\settings.py". I now
start the development server this way (in a Windows command
line window):
cd D:\dproj\MSQall\website\GoogleCodeHost
set DJANGO_SETTINGS_MODULE=GoogleCodeHost.settings
python manage.py runserver 6800
There is no difference. Besides these are positively read
from file "settings.py":
DATABASE_NAME
INSTALLED_APPS
TEMPLATE_DIRS
MIDDLEWARE_CLASSES
"django-admin.py startproject XYZ" does not create file
"settings.py" containing DATETIME_FORMAT
or DATE_FORMAT
.
Perhaps there is a reason for that?
The sequence "d:", "cd D:\dproj\MSQall\website\GoogleCodeHost",
"python manage.py
shell", "from django.conf import settings",
"settings.DATE_FORMAT", "settings.DATETIME_FORMAT" outputs
(as expected):
'Y-m-d H:i:sO'
'Y-m-d'
So the content of file "settings.py" is being read, but does
not take effect in the Django Admin interface.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用:
DATE_TIME
生效,因为 l10n 的本地化会覆盖DATETIME_FORMAT
和DATE_FORMAT
,如以下文档所述:https://docs.djangoproject.com/en/1.9/ref/settings/#date-formatWith:
DATE_TIME
takes effect, since the localization of l10n overridesDATETIME_FORMAT
andDATE_FORMAT
as documented at: https://docs.djangoproject.com/en/1.9/ref/settings/#date-format正如 Ciro Santilli 所说,当
USE_L10N = True
时,本地化格式会覆盖设置中的DATETIME_FORMAT
。 但您仍然可以通过 按照 Django 文档中的描述创建自定义格式文件。请参阅此处的详细答案。
As Ciro Santilli told, localization format overrides
DATETIME_FORMAT
in settings whenUSE_L10N = True
. But you can still overrideDATETIME_FORMAT
and other date/time formats by creating custom format files as described in Django documentation.See detailed answer here.
这将解决不可能的特定问题
与 DATETIME_FORMAT (因为它在当前 Django 中被忽略
尽管有文档,但实现也很脏,并且
类似于 ayaz 的答案(不太全局 - 只会影响
管理站点列表视图):
就在该行之后
(Django 通常位于文件夹 Lib/site-packages 中
Python 安装)
覆盖 datetime_format 的值(对于
模型中的 models.DateTimeField):
对于仅日期字段:
重新启动网络服务器(例如开发服务器)或
不需要退出管理界面
此更改生效。 在网络浏览器中进行简单的刷新
这就是所需要的一切。
This will solve the particular problem that is not possible
with DATETIME_FORMAT (as it is ignored in the current Django
implementations despite the documentation), is dirty too and
is similar to ayaz's answer (less global - will only affect
the admin site list view):
Right after the line
in file (Django is usually in folder Lib/site-packages in
the Python installation)
overwrite the value of datetime_format (for a
models.DateTimeField in the model):
And for date-only fields:
Restart of the web-server (e.g. development server) or
logging out of the admin interface is NOT necessary for
this change to take effect. A simple refresh in the web-browser
is all what is required.
这两个设置指令应在
settings.py
中定义。 您能否确保在启动开发服务器时读取您正在编辑的相同settings.py
?您始终可以通过运行 python manage.py shell 进入 Python 交互式 shell,然后运行这些命令以确保日期/时间格式值是否正常:
好的,我忘了查找它,但是 ticket #2203 处理这个问题。 不幸的是,该票证仍处于待处理状态。
我记得对于使用 Django 0.97 分支的某个主干修订版的项目,我通过覆盖
get_date_formats 中的
函数。 它很脏,但我已经在该项目中使用了某种自定义的 Django,因此在对其进行更多修改时没有发现任何问题。date_format
和datetime_format
值来解决这个问题django/utils/translation/trans_real.py
中的 ()The two setting directives should be defined in
settings.py
. Could you ensure that the samesettings.py
that you are editing is being read when you start the development server?You could always drop to the Python interactive shell by running
python manage.py shell
, and run these commands to ensure whether the date/time format values are getting through fine:Ok, I forgot to look it up, but ticket #2203 deals with this. Unfortunately, the ticket remains in pending state.
I remember that for a project that used a certain trunk revision of the 0.97 branch of Django, I worked around that by overwriting the
date_format
anddatetime_format
values in theget_date_formats()
function insidedjango/utils/translation/trans_real.py
. It was dirty, but I had already been using a custom Django of sorts for that project, so didn't see anything going wrong in hacking it trifle more.根据我对 Django 4.2.1 的实验,DATETIME_FORMAT 为 DateTimeField()仅适用于 Django 模板。 此外,DATE_FORMAT 为 DateField() 和 TIME_FORMAT for TimeField() 在 Django 模板中工作。 *您可以查看我的问题解释一下。
并且, DATE_INPUT_FORMATS 为
DateTimeField( )
和DateField()
和 TIME_INPUT_FORMATS 用于DateTimeField()
和TimeField()
在 Django Admin 中工作。 *DATETIME_INPUT_FORMATS 不适用于任何部分Django 项目的,您可以看到 我的问题解释了它。*您需要设置 USE_L10N
False< /code> 在
settings.py
中使上述设置起作用,因为USE_L10N
位于它们之前。According to my experiments with Django 4.2.1, DATETIME_FORMAT for DateTimeField() works only in Django Templates. In addition, DATE_FORMAT for DateField() and TIME_FORMAT for TimeField() work in Django Templates. *You can see my question explaining it.
And, DATE_INPUT_FORMATS for
DateTimeField()
andDateField()
and TIME_INPUT_FORMATS forDateTimeField()
andTimeField()
work in Django Admin. *DATETIME_INPUT_FORMATS doesn't work to any parts of a Django project and you can see my question explaining it.*You need to set USE_L10N
False
insettings.py
to make these settings above work becauseUSE_L10N
is prior to them.