def edit_timer(request, timer_id): """ Serves URL /op/timers/edit/<id>/ Edit an existing timer. """ if request.method == 'POST': form = TimerForm(request.POST) if form.is_valid(): # Fetch the timer object for given id timer = Timer.objects.get(pk=timer_id) # populate form with post data for given instance form = TimerForm(request.POST, instance=timer) # Save changes to DB form.save() # Redirect to timer overview page return HttpResponseRedirect('/op/timers') else: # Timer form with populated data try: # Get the timer object by id timer = get_object_or_404(Timer, id=int(timer_id)) # Create the form form = TimerForm(instance=timer) # Create the form context context = RequestContext(request, {'form': form, 'id': timer_id}) return render_to_response("ecm/op/edit_timer.html", context) except ValueError: raise Http404()
def add_timer(request): if request.method == 'POST': form = TimerForm(request.POST) if form.is_valid(): form.save() # Redirect to timer overview page return HttpResponseRedirect('/op/timers') else: form = TimerForm() # Create the form context context = RequestContext(request, {'form': form,}) return render_to_response("ecm/op/add_timer.html", context)