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 %}