python - calling a Django view function from django HTML template -


i'm trying call last function in code (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]      # display upcoming classes 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 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)                         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 x in course.schedule if x 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 

into html template

    <h4 class="t2">{{ weeks_per_course }} week {{ productdetailview.get_level_msg }}<!-- beginner intensive --></h4> 

using examples i've found on web, said should first use model, in case productdetailview. i'm 3 days django , still don't understand how this! can me reach conclusion? thanks!

i don't understand why want function here @ all, or why want called template. value depends on value within get_context_data; should use basic if statement within method.

... 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 

now can refer {{ get_level_msg }} directly in template, other value.

(i doubt template expect, or present @ all, though; suspect should using self.object.)


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -