def event_edit(request, slug): event = get_object_or_404( Event.objects, slug__exact=slug, user=request.user) if not event.is_visible_by(request.user): raise Http404 if request.method == 'POST': form = EventForm(request.POST, instance=event) formset = EventURLFormSet(request.POST, instance=event) if form.is_valid() and formset.is_valid(): instance = form.save() formset.save() messages.success( request, 'The event "%s" has been updated.' % instance.name) return redirect(instance.get_absolute_url()) else: # Activate the current event timezone: timezone.activate(pytz.timezone(event.timezone)) form = EventForm(instance=event) formset = EventURLFormSet(instance=event) context = { 'object': event, 'form': form, 'formset': formset, } return TemplateResponse(request, 'events/object_edit.html', context)
def event_add(request): """Form to add an ``Event``. Only authenticated users can add events.""" if request.method == 'POST': form = EventForm(request.POST) formset = EventURLFormSet(request.POST) if form.is_valid() and formset.is_valid(): instance = form.save(commit=False) instance.user = request.user instance.save() form.save_m2m() # Save URL inline form: formset.instance = instance formset.save() messages.success( request, 'The event "%s" has been added.' % instance.name) return redirect(instance.get_absolute_url()) else: form = EventForm() formset = EventURLFormSet() context = { 'form': form, 'formset': formset, } return TemplateResponse(request, 'events/object_add.html', context)