Skip to content

Instantly share code, notes, and snippets.

Created May 1, 2016

What would you like to do?
class WriteReview(StudentOnlyMixin, CreateView):
model = CourseReview
form_class = ReviewForm
template_name = 'academy/write_review.html'
def dispatch(self, request, *args, **kwargs):
# check if review exists
course = get_object_or_404(Course, slug=self.kwargs['slug'])
self.course = course
old_review = CourseReview.objects.filter(author=self.request.user, course=course).first()
except IndexError:
return HttpResponseRedirect()
return super(WriteReview, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(WriteReview, self).get_context_data(**kwargs)
context['course'] = get_object_or_404(Course, slug=self.kwargs['slug'])
return context
def form_valid(self, form):
course = get_object_or_404(Course, slug=self.kwargs['slug'])
form.instance.course = course = self.request.user
messages.success(self.request, 'Your review has been saved!')
return super(WriteReview, self).form_valid(form)
def get_success_url(self):
return "{}?#LECTURES".format(reverse('', args={self.kwargs['slug']}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.