Set event form initial values in init instead of constructor arguments
This commit is contained in:
parent
ff42396208
commit
e02ba33348
@ -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
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user