From a1247c46b02c89a7d80fed36c89ce8476d0eac79 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 29 Aug 2013 11:24:37 +0200 Subject: [PATCH] Forum: related lesson fixes - lesson should not be required - handle missing lesson better --- forum/forms.py | 2 +- forum/templates/forum/related_lesson_info.html | 4 +++- forum/views.py | 9 ++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/forum/forms.py b/forum/forms.py index 559796b..ec8a3be 100644 --- a/forum/forms.py +++ b/forum/forms.py @@ -6,5 +6,5 @@ from catalogue.models import Lesson class PostForm(pybb.forms.PostForm): - lesson = forms.ModelChoiceField(label = _('Related lesson'), queryset = Lesson.objects.all()) + lesson = forms.ModelChoiceField(label = _('Related lesson'), queryset = Lesson.objects.all(), required = False) \ No newline at end of file diff --git a/forum/templates/forum/related_lesson_info.html b/forum/templates/forum/related_lesson_info.html index bbfb686..1f807f6 100644 --- a/forum/templates/forum/related_lesson_info.html +++ b/forum/templates/forum/related_lesson_info.html @@ -1,2 +1,4 @@ {% load i18n %} -
{% trans 'Related lesson' %}: {{lesson.title}}
\ No newline at end of file +{% if lesson %} +
{% trans 'Related lesson' %}: {{lesson.title}}
+{% endif %} \ No newline at end of file diff --git a/forum/views.py b/forum/views.py index 7eb412e..41b7eb8 100644 --- a/forum/views.py +++ b/forum/views.py @@ -1,6 +1,9 @@ +from django.core.exceptions import ObjectDoesNotExist import pybb.views import pybb.forms +from catalogue.models import Lesson + from .forms import PostForm from .models import Topic @@ -48,5 +51,9 @@ class EditPostView(PostEditMixin, pybb.views.EditPostView): def get_form_kwargs(self): kwargs = super(EditPostView, self).get_form_kwargs() - kwargs['initial']['lesson'] = self.object.topic.edumed_topic.lesson + try: + lesson = self.object.topic.edumed_topic.lesson + except ObjectDoesNotExist: + lesson = None + kwargs['initial']['lesson'] = lesson return kwargs -- 2.20.1