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 django import forms
|
||||||
|
|
||||||
from .models import Event
|
from .models import Event
|
||||||
@ -6,3 +8,10 @@ class EventForm(forms.ModelForm):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = Event
|
model = Event
|
||||||
fields = ['date', 'user', 'notes']
|
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.shortcuts import get_object_or_404, render
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
@ -33,17 +31,14 @@ def taskDetail(request, asset_tag, task_slug):
|
|||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
if request.user.is_authenticated:
|
if request.user.is_authenticated:
|
||||||
event = Event(task=task)
|
event = Event(task=task)
|
||||||
form = EventForm(request.POST, instance=event)
|
form = EventForm(request.POST, user=request.user, instance=event)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
form.save()
|
||||||
else:
|
else:
|
||||||
form = EventForm(request.POST)
|
form = EventForm(request.POST)
|
||||||
form.add_error(None, "Not authenticated: please login")
|
form.add_error(None, "Not authenticated: please login")
|
||||||
else:
|
else:
|
||||||
form = EventForm(initial={
|
form = EventForm(user=request.user)
|
||||||
'date': datetime.now(),
|
|
||||||
'user': request.user,
|
|
||||||
})
|
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
'tool': tool,
|
'tool': tool,
|
||||||
|
Loading…
Reference in New Issue
Block a user