如何制作 Django 的“DATETIME_FORMAT” 积极的?

发布于 2024-07-24 09:46:40 字数 2077 浏览 4 评论 0原文

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."

更新 1DATETIME_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_FORMATDATE_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

勿忘初心 2024-07-31 09:46:40

使用:

USE_L10N = False

DATE_TIME 生效,因为 l10n 的本地化会覆盖 DATETIME_FORMATDATE_FORMAT,如以下文档所述:https://docs.djangoproject.com/en/1.9/ref/settings/#date-format

With:

USE_L10N = False

DATE_TIME takes effect, since the localization of l10n overrides DATETIME_FORMAT and DATE_FORMAT as documented at: https://docs.djangoproject.com/en/1.9/ref/settings/#date-format

南风起 2024-07-31 09:46:40

正如 Ciro Santilli 所说,当 USE_L10N = True 时,本地化格式会覆盖设置中的 DATETIME_FORMAT。 但您仍然可以通过 按照 Django 文档中的描述创建自定义格式文件

请参阅此处的详细答案。

USE_L10N = True 时,您可以覆盖 DATE_FORMATDATETIME_FORMATTIME_FORMAT 和其他日期/时间格式按照 Django 文档中所述创建自定义格式文件.

总结:

  1. settings.py 中设置 FORMAT_MODULE_PATH = 'yourproject.formats'
  2. 创建目录结构 yourproject/formats/en(如果您使用英语以外的其他区域设置,请将 en 替换为相应的 ISO 639-1 区域设置代码)并添加 __init__.py 文件到所有目录,使其成为有效的 Python 模块
  3. formats.py 添加到叶目录,其中包含要覆盖的格式定义,例如 DATE_FORMAT = 'j. F Y'

来自实际项目的示例 在这里

As Ciro Santilli told, localization format overrides DATETIME_FORMAT in settings when USE_L10N = True. But you can still override DATETIME_FORMAT and other date/time formats by creating custom format files as described in Django documentation.

See detailed answer here.

You can override DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT and other date/time formats when USE_L10N = True by creating custom format files as described in Django documentation.

In summary:

  1. Set FORMAT_MODULE_PATH = 'yourproject.formats' in settings.py
  2. Create directory structure yourproject/formats/en (replacing en with the corresponding ISO 639-1 locale code if you are using other locale than English) and add __init__.py files to all directories to make it a valid Python module
  3. Add formats.py to the leaf directory, containing the format definitions you want to override, e.g. DATE_FORMAT = 'j. F Y'.

Example from an actual project here.

半葬歌 2024-07-31 09:46:40

这将解决不可能的特定问题
与 DATETIME_FORMAT (因为它在当前 Django 中被忽略
尽管有文档,但实现也很脏,并且
类似于 ayaz 的答案(不太全局 - 只会影响
管理站点列表视图):

就在该行之后

(日期格式,日期时间格式,时间格式)= get_date_formats()

(Django 通常位于文件夹 Lib/site-packages 中
Python 安装)

django/contrib/admin/templatetags/admin_list.py

覆盖 datetime_format 的值(对于
模型中的 models.DateTimeField):

datetime_format = 'Ymd H:i:sO'

对于仅日期字段:

日期格式 = 'Ymd'

重新启动网络服务器(例如开发服务器)或
不需要退出管理界面
此更改生效。 在网络浏览器中进行简单的刷新
这就是所需要的一切。

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

(date_format, datetime_format,time_format) = get_date_formats()

in file (Django is usually in folder Lib/site-packages in
the Python installation)

django/contrib/admin/templatetags/admin_list.py

overwrite the value of datetime_format (for a
models.DateTimeField in the model):

datetime_format = 'Y-m-d H:i:sO'

And for date-only fields:

date_format = 'Y-m-d'

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.

层林尽染 2024-07-31 09:46:40

这两个设置指令应在 settings.py 中定义。 您能否确保在启动开发服务器时读取您正在编辑的相同 settings.py

您始终可以通过运行 python manage.py shell 进入 Python 交互式 shell,然后运行这些命令以确保日期/时间格式值是否正常:

from django.conf import settings
settings.DATE_FORMAT
settings.DATETIME_FORMAT

好的,我忘了查找它,但是 ticket #2203 处理这个问题。 不幸的是,该票证仍处于待处理状态。

我记得对于使用 Django 0.97 分支的某个主干修订版的项目,我通过覆盖 get_date_formats 中的 date_formatdatetime_format 值来解决这个问题django/utils/translation/trans_real.py 中的 () 函数。 它很脏,但我已经在该项目中使用了某种自定义的 Django,因此在对其进行更多修改时没有发现任何问题。

The two setting directives should be defined in settings.py. Could you ensure that the same settings.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:

from django.conf import settings
settings.DATE_FORMAT
settings.DATETIME_FORMAT

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 and datetime_format values in the get_date_formats() function inside django/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.

淡淡離愁欲言轉身 2024-07-31 09:46:40

根据我对 Django 4.2.1 的实验,DATETIME_FORMATDateTimeField()仅适用于 Django 模板。 此外,DATE_FORMATDateField()TIME_FORMAT for TimeField() 在 Django 模板中工作。 *您可以查看我的问题解释一下。

并且, DATE_INPUT_FORMATSDateTimeField( )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() and DateField() and TIME_INPUT_FORMATS for DateTimeField() and TimeField() 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 in settings.py to make these settings above work because USE_L10N is prior to them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文