def edit_homework(request, subject_name, homework_id): """ This view displays a form to edit a homework resp. saves a homework using the form data. """ homework = get_homework_or_404(subject_name, homework_id) # Check if a `next_view` was specified in POST data # (e.g. when clicking "edit" from a single displayed homework) if 'next_view' in request.POST.keys(): next_view = request.POST['next_view'] else: next_view = False if not request.POST or len(request.POST) == 3: form = HomeworkForm(instance=homework) else: form = HomeworkForm(request.POST) if form.is_valid(): homework.short_description = form.cleaned_data['short_description'] homework.long_description = form.cleaned_data['long_description'] homework.date_ends = form.cleaned_data['date_ends'] homework.subject = form.cleaned_data['subject'] homework.save() request.user.message_set.create(message="Your homework was saved.") if next_view: return HttpResponseRedirect(reverse(next_view, args=[homework.subject.name, homework.id])) else: return HttpResponseRedirect(reverse('hw_list_all')) data = { 'form': form, 'new': False, 'homework': homework, 'next_view': next_view } return render_to_response( 'homeworkmanager/edit.html', data, context_instance = RequestContext(request), )
def create_homework(request): """ This view displays a form for creating a homework resp. creates a homework using the form data. """ form = HomeworkForm(request.POST or None) if form.is_valid(): homework = form.save() request.user.message_set.create(message="Homework was created.") return HttpResponseRedirect(reverse('hw_list_all')) data = { 'form': form, 'new' : True, 'homework': None } return render_to_response( 'homeworkmanager/edit.html', data, context_instance = RequestContext(request), )