1

I use DateTimeField(), DateField() and TimeField() in MyModel class as shown below. *I use Django 4.2.1:

# "models.py"

from django.db import models

class MyModel(models.Model):
    datetime = models.DateTimeField() # Here
    date = models.DateField() # Here
    time = models.TimeField() # Here

Then, I set DATE_INPUT_FORMATS and TIME_INPUT_FORMATS and set USE_L10N False to make DATE_INPUT_FORMATS and TIME_INPUT_FORMATS work in settings.py as shown below:

# "settings.py"

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = False # Here

USE_TZ = True

DATE_INPUT_FORMATS = ["%m/%d/%Y"] # '10/25/2023' # Here
TIME_INPUT_FORMATS = ["%H:%M"] # '14:30' # Here

Then, DATE_INPUT_FORMATS and TIME_INPUT_FORMATS work in Django Admin as shown below:

enter image description here

Next, I set DATETIME_INPUT_FORMATS and set USE_L10N False to make DATETIME_INPUT_FORMATS work in settings.py as shown below:

# "settings.py"

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = False # Here

USE_TZ = True

DATETIME_INPUT_FORMATS = ["%m/%d/%Y %H:%M"] # '10/25/2023 14:30'

But, DATETIME_INPUT_FORMATS does not work in Django Admin as shown below:

enter image description here

In addition, from a MyModel objcet, I get and print datetime, date and time and pass them to index.html in test() in views.py as shown below:

# "views.py"

from django.shortcuts import render
from .models import MyModel

def test(request):
    obj = MyModel.objects.all()[0]
    print(obj.datetime)
    print(obj.date)
    print(obj.time)
    return render(
        request, 
        'index.html', 
        {"datetime": obj.datetime, "date": obj.date, "time": obj.time}
    )

But, all DATE_INPUT_FORMATS, TIME_INPUT_FORMATS and DATETIME_INPUT_FORMATS don't work according to console as shown below:

2023-10-25 14:30:15+00:00
2023-10-25
14:30:15

Next, I show datetime, date and time in index.html as shown below:

# "index.html"

{{ datetime }}<br/>
{{ date }}<br/>
{{ time }} 

But, all DATE_INPUT_FORMATS, TIME_INPUT_FORMATS and DATETIME_INPUT_FORMATS don't work according to browser as shown below:

Oct. 25, 2023, 2:30 p.m.
Oct. 25, 2023
2:30 p.m.

My questions:

  1. How can I make DATETIME_INPUT_FORMATS work in Django Admin?
  2. Why doesn't DATETIME_INPUT_FORMATS work in Django Admin?
  3. If DATETIME_INPUT_FORMATS doesn't work in Django Admin, where does DATETIME_INPUT_FORMATS work?
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129

0 Answers0