im making a webapp, im making a DateTime table, I have done this before but without any problem
forms.py
class DateInput(forms.DateInput):
input_type = 'date'
class HomeworkForm(forms.ModelForm):
class Meta:
model = Homework
Widgets = {'due':DateInput(attrs={'type':'date'})}
fields = ['subject','title','description','due','is_finished']
models.py
class Homework(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
subject = models.CharField(max_length=50)
title = models.CharField(max_length=100)
description = models.TextField()
due = models.DateTimeField()
is_finished = models.BooleanField(default=False)
views.py
def homework(request):
if request.method == 'POST':
form = HomeworkForm(request.POST)
if form.is_valid():
try:
finished = request.POST['is_finished']
if finished == 'on':
finished = True
else:
finished = False
except:
finished = False
homework = Homework(
user = request.user ,
subject = request.POST['subject'],
title = request.POST['title'],
description = request.POST['description'],
due = request.POST['due'],
is_finished = finished
)
homework.save()
messages.success(request,f'Homework Added from {request.user.username}!!')
else:
form = HomeworkForm()
homework = Homework.objects.filter(user=request.user)
if len(homework) == 0:
homework_done = True
else:
homework_done = False
context = {
'homeworks':homework,
'homeworks_done':homework_done,
'form':form,
}
return render(request,'dashboard/homework.html', context)
Ive used many ways to show the DateTime table in 'due' , where i can choose date from something like a calendar but i still got nothing and when i input a date myself f.e(2020-2-1)i see an error telling that the date form is invalid
CodePudding user response:
I am using something like:
widgets = {
'datetime_field': DateTimeInput(format='%Y-%m-%d' 'T' '%H:%M', attrs={'type': 'datetime-local'}),
'date_field': DateTimeInput(format='%Y-%m-%d', attrs={'type': 'date'}),
}
CodePudding user response:
I am using TimeStampedModel your code will be:
from django_extensions.db.models import TimeStampedModel
class Homework(TimeStampedModel):
user = models.ForeignKey(User,on_delete=models.CASCADE)
subject = models.CharField(max_length=50)
title = models.CharField(max_length=100)
description = models.TextField()
due = models.DateTimeField()
is_finished = models.BooleanField(default=False)
and then just run makemigration
and migrate
you will have two fields in your model, created & modified you can use them