How do I make Django's DATETIME_FORMAT active?

前端 未结 4 1995
渐次进展
渐次进展 2021-02-13 23:49

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

相关标签:
4条回答
  • 2021-02-13 23:56

    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

    0 讨论(0)
  • 2021-02-13 23:59

    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.

    0 讨论(0)
  • 2021-02-14 00:12

    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.

    0 讨论(0)
  • 2021-02-14 00:19

    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.

    0 讨论(0)
提交回复
热议问题