def add_lesson(request, id): course = Course.objects.get(id=id) form = LessonModelForm(initial={'course': course}) if request.method == 'POST': form = LessonModelForm(request.POST) if form.is_valid(): lesson = form.save() messages.success(request, ('Lesson %s has been successfully added.' % lesson.subject)) return redirect('courses:detail', lesson.course.id) context = {'form': form, 'course': course} return render(request, 'courses/add_lesson.html', context)
def add_lesson(request, id_of_course): if request.method == 'POST': form = LessonModelForm(request.POST) if form.is_valid(): lesson = form.save() messages.success( request, "Lesson %s has been successfully added." % (lesson.subject)) return redirect('courses:detail', id_of_course=id_of_course) else: form = LessonModelForm(initial={'course': id_of_course}) return render(request, 'courses/add_lesson.html', {'form': form})
def add_lesson(request, course_id): get_object_or_404(Course, pk=course_id) if request.method == 'POST': form = LessonModelForm(request.POST) if form.is_valid(): lesson = form.save() messages.success( request, 'Lesson %s has been successfully added.' % lesson.subject) return redirect( reverse('courses:detail', kwargs={'course_id': course_id})) else: form = LessonModelForm(initial={'course': course_id}) return render(request, 'courses/add_lesson.html', {'form': form})
def add_lesson(request, pk): course = Course.objects.get(id=pk) if request.method == "POST": form = LessonModelForm(request.POST, initial = {'course':course.id}) if form.is_valid(): data = form.cleaned_data form.save() messages.success(request, 'Lesson {} has been successfully added.'.format(data['subject'])) return redirect('courses:detail', pk) else: form = LessonModelForm() return render(request, "courses/add_lesson.html", {'form':form})
def get(self, request, *args, **kwargs): course = Course.objects.get(pk=kwargs['pk']) form = LessonModelForm(initial={'course': course.id}) return render(request, self.template_name, {"form": form})