我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用django.views.generic.DetailView()。
def get_context_data(self,**kwargs): instructor = self.object context = {} context.update({ 'instructor': instructor, 'prior_series': Event.objects.filter(startTime__lte=timezone.now(),eventstaffmember__staffMember=instructor).order_by('-startTime'), 'upcoming_series': Event.objects.filter(startTime__gt=timezone.now(),eventstaffmember__staffMember=instructor).order_by('-startTime'), }) if context['prior_series']: context.update({'first_series': context['prior_series'].last(),}) context.update({ 'teaching_since': month_name[context['first_series'].month] + ' ' + str(context['first_series'].year), 'student_count': sum([x.numRegistered for x in context['prior_series']]), }) context.update({'series_count': len(context['prior_series']) + len(context['upcoming_series'])}) # Note: This get the detailview's context, not all the mixins. Supering itself led to an infinite loop. return super(DetailView, self).get_context_data(**context)
def get_context_data(self, **kwargs): context = super(DetailView, self).get_context_data(**kwargs) # for_user = self.request.user user_form = UserForm(data=self.request.GET) if user_form.is_valid(): for_user = user_form.cleaned_data['user'] else: for_user = None context.update( categories=Category.objects.all(), for_user=for_user, user_form=user_form, VARIANCE_CUTOFF=defaults.VARIANCE_CUTOFF ) return context
def get_object(self, queryset=None): """Ensure DetailView operates on the right queryset when fetching object. Since this is a StreamMixin view, we must override get_queryset which is for the content. To ensure DetailView works, we override get_object to pass a Profile queryset to get_object up in super. """ return super().get_object(queryset=Profile.objects.all())
def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(DetailView, self).get_context_data(**kwargs) # Add in a QuerySet of all the books context['kategorije'] = Kategorija.objects.all() return context
def get_queryset(self): teacher_list = self.model.objects.filter( recommended_on_wechat=True ).filter( published=True ) return teacher_list # class TeacherDetailView(DetailView): # model = models.Teacher
def get_context_data(self, **kwargs): # This overrides the get_context_data() method. context = super(Developer_detail, self).get_context_data(**kwargs) # This allows calling the method of the super class. Without this line we would not have the basic context. tasks_dev = Task.objects.filter(developer = self.object) # This allows us to retrieve the list of tasks developer. We use self.object, which is a Developer type object already defined by the DetailView class. context['tasks_dev'] = tasks_dev # In this line, we add the task list to the context. return context
def get_context_data(self, **kwargs): context = super(DetailView, self).get_context_data(**kwargs) fw_name = context['object'].name try: details = Features.objects.get(library_framework_name=fw_name) fields = [(i.help_text,getattr(details, i.name)) for i in details._meta.fields if len(i.help_text) > 0] context['details'] = fields except: pass context['question_count'] = QuestionOnIt.get_count_for_lang(fw_name).count context['jobs'] = Job.get_all_job_for(fw_name) context['courses'] = Course.get_courses_for_lang(fw_name) return context
def get_context_data(self, **kwargs): context = super(DetailView, self).get_context_data(**kwargs) return context
def get_context_data(self, **kwargs): context = super(DetailView, self).get_context_data(**kwargs) pk = kwargs['object'].course_id if kwargs['object'].source == 'COURSERA': context['partners'] = CoursePartner.get_course_partner(pk) return context # REST
def dispatch(self, request, *args, **kwargs): """Overriding dispatch on DetailView""" self.model = get_model(**kwargs) return super(EntityDetail, self).dispatch( request, *args, **kwargs)
def api_player_eurochicken_catch(request, player_id, code): # GET /api/player/:ID/eurochicken/:CODE/ try: player = Player.objects.get(id=player_id) player.setup_eurochicken_if_needed() except: response = JsonResponse({"error_message":"player with id %s not found" % player_id}) response.status_code = 404 return response if player.code_eurochicken != code: response = JsonResponse({"error_message":"player cannot use eurochicken code %s" % code}) response.status_code = 400 return response (catch, just_caught) = player.pickup_eurochicken_catch(code) if not catch: response = JsonResponse({"error_message":"player cannot use eurochicken code %s" % code}) response.status_code = 400 return response return JsonResponse({'catch':catch.json_public_full(), 'is_new_catch':just_caught}, json_dumps_params={'indent': 2}) #class DetailView(generic.DetailView): # model = Egg # template_name = 'egg_detail.html'
def get_context_data(self, **kwargs): context = super(DetailView, self).get_context_data(**kwargs) context['fields'] = self.get_fields(context['object']) context['links'] = self.get_links() return context
def get_context_data(self, **kwargs): """Override this method in order to get services.""" context = super(DetailView, self).get_context_data(**kwargs) context['services'] = context['resource'].services.all() return context
def get_context_data(self, **kwargs): context = super(DetailView, self).get_context_data(**kwargs) # ????????????? if self.request.user.is_authenticated and self.request.user == self.object.user: context['editable'] = True return context