Django的DATETIME_FORMAT如何激活? [英] How do I make Django's DATETIME_FORMAT active?

查看:191
本文介绍了Django的DATETIME_FORMAT如何激活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django管理站点
(Django的自动管理界面)中显示日期时间,DATETIME_FORMAT应该放在哪里才能生效



DATETIME_FORMAT的文档,第
http:// docs.djangoproject.com/en/1.0/ref/settings/ 说:

 默认格式为用于
Django管理员更改列表页面上的日期时间字段,以及可能由
系统的其他部分使用。

更新1 :DATETIME_FORMAT已损坏(其值为
忽略),尽管有文档。许多年前,它
工作,但从那时起Django实现已经
破碎wrt。这个功能。似乎Django社区
无法决定如何解决它(但在此期间,我认为他们
应从文档中删除DATETIME_FORMAT或添加
a注意这个问题)。 / p>

我已将这些行放入
网站/项目(而不是应用程序)的文件settings.py中,但似乎没有
任何效果(重新启动开发服务器后):


DATETIME_FORMAT ='Ymd H:i:sO'



DATE_FORMAT ='Ym-d'


作为示例2009年6月29日,7: 30点当
使用Django管理站点时显示。



Django版本是1.0.2 final,Python版本是2.6.2
(64位)。平台:Windows XP 64位。



堆栈溢出问题 欧洲日期输入Django管理员 似乎是完全相反的问题(因此一个明显的
矛盾)。



文件的完整路径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



INSTALLED_APPS



TEMPLATE_DIRS



MIDDLEWARE_CLASSES


django-admin.py startproject XYZ不会创建包含DATETIME_FORMAT或DATE_FORMAT的文件
settings.py。
也许有原因?



序列d:,cd D:\dproj\MSQall\website\GoogleCodeHost
python manage.py
shell,from django.conf import settings,
settings.DATE_FORMAT,settings.DATETIME_FORMAT输出
):

 'Ymd H:i:sO'
'Ym-d'

所以文件settings.py的内容正在读取,但
在Django Admin界面中没有生效

解决方案

这将解决具有DATETIME_FORMAT的不可能的
的特定问题(因为它在当前被忽略Django
执行尽管有文档),也是脏的,
类似于ayaz的答案(较少的全局 - 只会影响
的管理员站点列表视图):



在线后面


(date_format,datetime_format,time_format)= get_date_formats()

$ b $文件中的b

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







$ b

覆盖datetime_format(的值),

django / contrib / admin / templatetags / admin_list.py


对于
models.DateTimeField在模型中):


datetime_format ='Ymd H:i:sO'


对于仅限日期的字段:


date_format = 'Ym-d'


重新启动Web服务器(例如开发服务器)或
注销管理界面是
必需的此更改生效。网路浏览器
的简单刷新就是所需要的。


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.

解决方案

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.

这篇关于Django的DATETIME_FORMAT如何激活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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