diff --git a/tasks/forms.py b/tasks/forms.py index 0e03d29..99f1fe3 100644 --- a/tasks/forms.py +++ b/tasks/forms.py @@ -1,3 +1,5 @@ +from datetime import datetime + from django import forms from .models import Event @@ -6,3 +8,10 @@ class EventForm(forms.ModelForm): class Meta: model = Event fields = ['date', 'user', 'notes'] + + def __init__(self, *args, **kwargs): + user = kwargs.pop('user') + super().__init__(*args, **kwargs) + + self.fields['date'].initial = datetime.now() + self.fields['user'].initial = user diff --git a/tasks/views.py b/tasks/views.py index 8d08729..f524f90 100644 --- a/tasks/views.py +++ b/tasks/views.py @@ -1,5 +1,3 @@ -from datetime import datetime - from django.shortcuts import get_object_or_404, render from django.http import HttpResponse @@ -33,17 +31,14 @@ def taskDetail(request, asset_tag, task_slug): if request.method == 'POST': if request.user.is_authenticated: event = Event(task=task) - form = EventForm(request.POST, instance=event) + form = EventForm(request.POST, user=request.user, instance=event) if form.is_valid(): form.save() else: form = EventForm(request.POST) form.add_error(None, "Not authenticated: please login") else: - form = EventForm(initial={ - 'date': datetime.now(), - 'user': request.user, - }) + form = EventForm(user=request.user) context = { 'tool': tool,