Home > other >  Djano value has an invalid format. It must be in YYYY-MM-DD HH:MM
Djano value has an invalid format. It must be in YYYY-MM-DD HH:MM

Time:10-06

for example when I use auto_now_add=True "Jan. 1, 2021, 5:05 a.m." format, but when I want to update the date, I get the error "YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']", I don't have a problem when I update it by entering in YYYY-MM-DD HH:MM format, but how can I bring this date automatically in this format?

models.py;

class problemduyuru(models.Model):
    olusturulmatarihi = models.DateTimeField(auto_now_add=True, blank=True)
    duyurutipi = models.TextField(max_length=100, null=True)
    incidentno = models.TextField(max_length=100, null=True)
    baslangiczamani = models.TextField(max_length=100, null=True)
    aciklama = models.TextField(max_length=100, null=True) 

views.py create and update;

update;

def problemduyurusuupdate(request, id):
    problemmember = problemduyuru.objects.get(id=id)
    problemmember.duyurutipi = request.POST['duyurutipi']
    problemmember.incidentno = request.POST['incidentno']
    problemmember.baslangiczamani = request.POST['baslangiczamani']
    problemmember.aciklama = request.POST['aciklama']
    problemmember.olusturulmatarihi = request.POST['olusturulmatarihi']
    problemmember.save()
    messages.success(request, 'Alarmlar was updated successfully!')
    return redirect('/problemduyurusu')

create;

def problemduyurusucreate(request):
    if request.method == 'POST':
        problemmember = problemduyuru(
            duyurutipi=request.POST['dduyurutipi'],
            incidentno=request.POST['dincidentno'],
            baslangiczamani=request.POST['dbaslangiczamani'],
            aciklama=request.POST['daciklama'],
            olusturulmatarihi=request.POST['dolusturulmatarihi'], )
                 
        try:
            problemmember.full_clean()
        except ValidationError as e:
            pass
        problemmember.save()
        messages.success(request, 'Alarmlar was created successfully!')
        return redirect('/problemduyurusu')
    else:
        return render(request, 'problemduyurusucreate.html')

CodePudding user response:

DateTimeField The field always accepts strings in ISO 8601 formatted dates or similar recognized by parse_datetime(). Some examples are:

  • '2006-10-25 14:30:59'
  • '2006-10-25T14:30:59'
  • '2006-10-25 14:30'
  • '2006-10-25T14:30'
  • '2006-10-25T14:30Z'
  • '2006-10-25T14:30 02:00'
  • '2006-10-25'

https://docs.djangoproject.com/en/dev/ref/forms/fields/#datetimefield

but if you want to bring this date automatically in this format you can use edit it in views.py (update)

problemmember.olusturulmatarihi = parse_datetime(request.POST['olusturulmatarihi'])

Parsing a Datetime String into a Django DateTimeField

https://docs.djangoproject.com/en/dev/ref/utils/#module-django.utils.dateparse

and edit template for what format you like to show

{{ value|date:"D d M Y" }}

https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#date

  • Related