Hey all,
I have an issue with the datetime-local widget. I have a model, “Order”, with a single datetime field. I specify the datetime-local widget to be used. When I go to create an Order, the widget works as expected. This saves and shows up properly in a detail view for that order. But, when I go to update that order, the date is not pre-populated in the form. (Screenshots available here since new users are limited to 1 image per post)
A full git repo with a site/app replicating this can be found here. But, for convenience, I’ll post the relevant sections here:
models.py
from django.db import models
# Create your views here.
class Order(models.Model):
intake_date_and_time = models.DateTimeField(blank=True, null=True)
views.py
from django.shortcuts import render
from django.views import generic, View
from django.urls import reverse
from .models import Order
from .forms import NewOrderForm
class CreateOrderView(generic.CreateView):
model = Order
template_name = "datetimepoc/create_order.html"
form_class = NewOrderForm
def get_success_url(self):
return reverse('datetimepoc/index')
class UpdateOrderView(generic.UpdateView):
model = Order
fields = "__all__"
template_name = "datetimepoc/create_order.html"
form_class = NewOrderForm
def get_success_url(self):
return reverse('datetimepoc/list_orders')
class OrderDetailView(generic.DetailView):
template_name = "datetimepoc/order_detail.html"
model = Order
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('detail/<int:pk>', views.OrderDetailView.as_view(), name='detail'),
path('create', views.CreateOrderView.as_view(), name='create_order'),
path('update/<int:pk>', views.UpdateOrderView.as_view(), name='update_order'),
]
forms.py
from django import forms
from .models import Order
class NewOrderForm(forms.ModelForm):
class Meta:
model = Order
widgets = {"intake_date_and_time": forms.DateTimeInput(attrs={'type':'datetime-local'}) }
fields = '__all__'
order_detail.html
<h1>{{ object.intake_date_and_time }}</h1>
create_order.html
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
Is this a bug? Is there a way to get it so that the update form has the date pre-populated with the value put in at creation (or last update)?