def new(request): '''Show form to add new subject''' if request.method == "POST": form = SubjectForm(request.POST) #prefilled form from POST data if form.is_valid(): new_subject = form.save() return HttpResponseRedirect("/subjecten/%s/" % new_subject.pk) else: form = SubjectForm() #empty form return render_to_response('subjecten/new_subject.html', {'form': form})
def edit(request, subject_id = None): '''Show form to change an existing subject''' if request.method == "POST": subject = Subject.objects.get(pk = subject_id) form = SubjectForm(request.POST, instance = subject) if form.is_valid(): new_subject = form.save() return HttpResponseRedirect("/subjecten/%s/" % new_subject.pk) else: subject = Subject.objects.get(pk = subject_id) form = SubjectForm(instance = subject) return render_to_response('subjecten/edit_subject.html', {'form': form, 'subject': subject})