Need help with Date Time Picker

I have created custom form and widget input but it still displays as text input instead of the widget

widgets.py

from django import forms

class DateTimePickerInput(forms.DateTimeInput):
        input_type = 'datetime'
forms.py

from .widgets import  DateTimePickerInput
from django.forms import ModelForm
from .models import Task

class ExampleForm(ModelForm):
    class Meta:
        model = Task
        fields = ['title', 'start_date', 'end_date', 'complete']

        widgets = {
            'start_date' : DateTimePickerInput(),
            'end_date' : DateTimePickerInput(),
        }
models.py

import uuid
from django.db import models
from django.contrib.auth.models import User




class Task(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, null = True, blank = True)
    title = models.CharField(max_length = 200)
    description = models.TextField(null = True, blank = True)
    complete = models.BooleanField(default= False)
    created = models.DateTimeField(auto_now_add = True)
    start_date = models.DateTimeField(null=True)
    end_date = models.DateTimeField(null=True)


    def __str__(self):
        return self.title

    class Meta:
        ordering = ['complete']
views.py 

from django.shortcuts import render, redirect
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView, FormView
from django.urls import reverse_lazy

from django.contrib.auth.views import LoginView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login

from .models import Task
from .forms import ExampleForm

def CreateTask(request):
    form = ExampleForm()
    context = {'form': form}
    return render(request, 'base/task_form.html', context)
urls.py

from django.urls import path
from . import views
from .views import TaskList, TaskDetail, TaskUpdate, DeleteView, CustomLoginView, RegisterPage , CreateTask #, TaskCreate 
from django.contrib.auth.views import LogoutView

urlpatterns =[
    path('login/', CustomLoginView.as_view(), name='login'),
    path('logout/', LogoutView.as_view(next_page= 'login'), name='logout'),
    path('register/', RegisterPage.as_view(), name='register'),

    path('', TaskList.as_view(), name='tasks'),
    path('task/<uuid:pk>/', TaskDetail.as_view(), name='task'),
    # path('task-create', TaskCreate.as_view(), name='task-create'),
    path('task-create', views.CreateTask, name='task-create'),
    path('task-update/<uuid:pk>/', TaskUpdate.as_view(), name='task-update'),
    path('task-delete/<uuid:pk>/', DeleteView.as_view(), name='task-delete'),
]

task_form.html

{% extends 'base/main_style.html' %}
{% block content %}
<h3>Task Form</h3>
<a href="{% url 'tasks' %}">Go back</a>

<form method="POST" action="">
    {% csrf_token %}
    <!-- {{form.as_p}} -->
    {{form}}

        <!-- <label for="title">Title:</label><br>
        {% if task.title != null %}
        <input type="text" id="title" name="title">{{task.title}}<br>
        {% else %}
        <input type="text" id="title" name="title"><br>
        {% endif %}
        <label for="start_date">start_date:</label><br>
        <input type="date" id="start_date" name="start_date">
  -->
    <!-- <input type="submit" value="Choose time now"> -->
    <input type="submit" value="Submit">
</form>
{% endblock content %}

I struggled with this too :slight_smile: Along the way I solved it and if I remember it was subclassing ModelForm rather than Form that did the trick.

Could you share your project implementation with datetime picker?

If you’re looking for specific assistance with code, please post the code here and not links to code elsewhere. (Posting links to external sites means that the code can go away at just about any time, reducing the value of the answers provided here. Also, code posted externally can’t be indexed or searched on this site.)

When you post code here, enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted, which is critical with Python.

1 Like

Thanks for pointing it out. Just fixed code to blocks.

I used widgets.DateInput() instead of DateTimePickerInput().

It seems to be working instead of using forms.DateTimeInput() to split them to two seperate ones.

class DateTimeInput(forms.DateTimeInput):
input_type = ‘datetime-local’

Try this
it worked for me

2 Likes

Is this still working? I copied the code above and still cant see the date time picker, even tried a different browser