Ad

Django Tempus Dominus Datetimepicker Time And Date Conversion

I've been trying for hours to change the format of my datetimepicker (Tempus DOminus for bootstrap 4) but the format won't change so now I'm trying to find a better way to convert it on the backend when it inserts to my Django model or database.

The issue is, the datetimepicker currently puts the user selected date into the input as

10/02/2018 2:15 PM

The problem is, I need to insert it into the database like so:

2018-10-02 02:15:00

Is there a better way that I can just auto convert that to fit the djano model timestamp format I need?

I'm thinking there would have to be a way for me to look at the AM/PM from the datetimepicker and make it either 02:00:00 or 14:00:00 accordingly.

in my model.py

from django.db import models

class CountryDirectorRequestTable(models.Model):
    countryDirectorRequestTo = models.CharField(max_length=50,choices=REQUEST_TO, default='accountant',verbose_name="Request To")
    countryDirectorRequestDetail = models.TextField(max_length=3000,null=False,blank=False, verbose_name="Request Detail",help_text="Content not more than 3000 letters")
    countryDirectorRequestExpectingDateAndTime = models.DateTimeField(default=timezone.now, verbose_name="Expecting Date and Time")

In my form.py

from django import forms
from Home.models import CountryDirectorRequestTable
from tempus_dominus.widgets import DateTimePicker

class CountryDirectorRequestForm(forms.ModelForm):
countryDirectorRequestExpectingDateAndTime = forms.DateTimeField(widget=DateTimePicker(
    options={
        'useCurrent': True,
        'collapse': False,
        'minDate': '2009-01-20',
        'maxDate': '2017-01-20',

        # Calendar and time widget formatting
        'time': 'fa fa-clock-o',
        'date': 'fa fa-calendar',
        'up': 'fa fa-arrow-up',
        'down': 'fa fa-arrow-down',
        'previous': 'fa fa-chevron-left',
        'next': 'fa fa-chevron-right',
        'today': 'fa fa-calendar-check-o',
        'clear': 'fa fa-delete',
        'close': 'fa fa-times'
    },
    attrs={
        'append': 'fa fa-calendar',
        'icon_toggle': True,

    }
)
)
class Meta:
    model = CountryDirectorRequestTable
    fields = ('countryDirectorRequestTo', 'countryDirectorRequestDetail','countryDirectorRequestExpectingDateAndTime')

in my views.py

from django.shortcuts import render, redirect
from .form import CountryDirectorRequestForm


def countryDirectorRequestView(request):
if request.method == 'POST':
    form = CountryDirectorRequestForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('countryDirector:countryDirectorRequest_detail')
else:
    form = CountryDirectorRequestForm()
return render(request, 'CountryDirector/countryDirectorRequest.html',{'form':form})

The date picker renders correctly, but after clicking the submit button I get validation error: "enter a valid date/time)

Ad

Answer

I was able to get this working. The problem I found was that minDate and maxDate have to be provided as datetime values instead of date values.

I.e., this is incorrect:

'minDate': '2009-01-20',
'maxDate': '2017-01-20',

Instead, this is correct:

'minDate': '2009-01-20 00:00:00',
'maxDate': '2017-01-20 23:59:59',

In the views, also be sure to use TestForm and not TestForm().

A friendly tip as well, you may want to look into PEP-8 syntax and the code formatter Black to learn about Python coding conventions. I've found it has saved me a lot of time, effort, and frustration over the years. https://black.readthedocs.io/en/stable/

Here's the working example:

models.py

from django.db import models
from django.utils import timezone


class CountryDirectorRequestTable(models.Model):
    countryDirectorRequestExpectingDateAndTime = models.DateTimeField(
        default=timezone.now,
        verbose_name="Expecting Date and Time",
    )

forms.py

from django import forms

from .models import CountryDirectorRequestTable

from tempus_dominus.widgets import DateTimePicker


class TestForm(forms.ModelForm):
    country_director = forms.DateTimeField(
        widget=DateTimePicker(
            options={
                'useCurrent': True,
                'collapse': False,
                'minDate': '2009-01-20 00:00:00',
                'maxDate': '2017-01-20 23:59:59',

                # Calendar and time widget formatting
                'time': 'fa fa-clock-o',
                'date': 'fa fa-calendar',
                'up': 'fa fa-arrow-up',
                'down': 'fa fa-arrow-down',
                'previous': 'fa fa-chevron-left',
                'next': 'fa fa-chevron-right',
                'today': 'fa fa-calendar-check-o',
                'clear': 'fa fa-delete',
                'close': 'fa fa-times'
            },
            attrs={
                'append': 'fa fa-calendar',
                'icon_toggle': True,
            }
        )
    )

    class Meta:
        model = CountryDirectorRequestTable
        fields = (
            'country_director',
        )

views.py

from django.shortcuts import render, redirect
from .forms import TestForm


def test_view(request):
    if request.method == 'POST':
        form = TestForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('test-form')
    else:
        form = TestForm

    return render(
        request,
        'test_app/test.html',
        {'form': form},
    )

I hope this helps - best of luck!

Ad
source: stackoverflow.com
Ad