我试图调用此代码中的最后一个函数(def get_level_msg():)
class ProductDetailView(ProductView):
model = EnrollableTemplate
@functional.cached_property
def location(self):
return get_object_or_404(
Location.actives, slug=self.kwargs['location_slug'])
def get_object(self):
return get_object_or_404(
EnrollableTemplate, slug=self.kwargs['product_slug'])
def get_template_names(self):
if self.get_object().category.language:
return 'pdp_language.html'
return 'immersion_pdp.html'
def get_faqs(self):
object = self.get_object()
if object.is_workshop:
return c.faqs['workshop']
elif object.category.language:
return c.faqs['language']
else:
return c.faqs['immersion']
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
template = kwargs[self.context_object_name]
# only display upcoming classes that have open seats available
courses = (template.enrollable_set
.upcoming()
.select_related('template', 'venue__location')
.filter(venue__location=self.location)
.order_by('start_date'))
loc_filter = self.request.GET.get('neighborhood')
context['language'] = language = template.category.language
if not self.object.is_workshop:
sessions = [enrollable.session_count for enrollable
in self.object.enrollable_set.upcoming()]
if sessions:
context['max_sessions'] = max(sessions)
neighborhoods = (Venue.objects.filter(location=self.location)
.filter(enrollable__in=courses)
.distinct()
.values_list('neighborhood', flat=True))
if loc_filter:
courses = courses.filter(course__venue__neighborhood=loc_filter)
context['filtered'] = True
location_filters = [('?neighborhood={}'.format(n), n) for
n in neighborhoods.iterator()]
path = urlparse(self.request.get_full_path()).path
location_filters.insert(0, ('{}#filter'.format(path), "all"))
context['filters'] = [('Location', location_filters)]
if language:
context['featured_teachers'] = self.get_featured_teachers(
self.location, language)
else:
try:
context['featured_teachers'] = [courses[0].teacher]
except IndexError:
context['featured_teachers'] = None
context['expectations'] = template.expectation_set.all()
context['faqs'] = self.get_faqs()
context['courses'] = courses
context['template_id'] = template.id
# import ipdb; ipdb.set_trace()
if courses.exists():
course = courses[0]
days_per_week = sum(1 for x in course.schedule if x is True)
if days_per_week == 1:
days_per_week = 'once'
elif days_per_week == 2:
days_per_week = 'twice'
else:
days_per_week = 'every day'
context['days_per_week'] = days_per_week
weeks = (course.start_date - course.end_date).days
context['weeks_per_course'] = abs(int(weeks / 7))
context['has_book'] = course.template.books.count() > 0
context['first_start_date'] = course.start_date
context['hello'] = template.level
level_message1 = "Beginner Course"
level_message2 = "Intermediate Course"
level_message3 = "Advanced Course"
level_message4 = "Beginner Intensive"
# context['get_level_msg'] =
def get_level_msg():
if template.level == 1:
return level_message1
elif template.level == 2:
return level_message1
elif template.level == 3:
return level_message1
elif template.level == 4:
return level_message2
elif template.level == 5:
return level_message2
elif template.level > 5:
return level_message3
else:
return level_message4
进入此HTML模板
<h4 class="t2">{{ weeks_per_course }} WEEK {{ ProductDetailView.get_level_msg }}<!-- BEGINNER INTENSIVE --></h4>
使用我在网上找到的示例,它说我应该首先使用该模型,在这种情况下是它的ProductDetailView。我进入django 3天后仍然不明白该怎么做!有人可以帮我得出结论吗?谢谢!
答案 0 :(得分:0)
我根本不明白为什么你想要一个函数,或者为什么要从模板中调用它。此值仅取决于get_context_data
内的其他值;你应该在该方法中使用基本的if
语句。
...
level_message1 = "Beginner Course"
level_message2 = "Intermediate Course"
level_message3 = "Advanced Course"
level_message4 = "Beginner Intensive"
if template.level == 1:
lvl = level_message1
elif template.level == 2:
lvl = level_message1
elif template.level == 3:
lvl = level_message1
elif template.level == 4:
lvl = level_message2
elif template.level == 5:
lvl = level_message2
elif template.level > 5:
lvl = level_message3
else:
lvl = level_message4
context['get_level_msg'] = lvl
现在,您可以直接在模板中引用{{ get_level_msg }}
,就像处理其他任何值一样。
(我怀疑template
实际上是你所期望的,甚至根本不存在;我怀疑你应该使用self.object
。)
答案 1 :(得分:0)
我意识到我没有正确地解决这个问题,这就是我要解决的问题:
t_level = template.level
if t_level == 1:
t_level = level_message1
elif t_level == 2:
t_level = level_message1
elif template.level == 3:
t_level = level_message1
elif template.level == 4:
t_level = level_message2
elif template.level == 5:
t_level = level_message2
elif template.level == 6:
t_level = level_message3
elif template.level == 7:
t_level = level_message3
else:
t_level = level_message4
context['t_level'] = t_level
我实际上是把它作为一个函数,这是没有必要的,所以我重写了它,所以我可以用上下文将它调用到我的模板中
<h4 class="t2">{{ weeks_per_course }} WEEK {{ t_level }}</h4>