Python TemplateView.get_template_name()和TemplateView.template_name返回不同的模板

Python TemplateView.get_template_name()和TemplateView.template_name返回不同的模板,python,django,django-views,Python,Django,Django Views,我试图根据QueryDict中包含或不包含的内容为TemplateView建立模板。当get\u template\u names()为真时,它将返回questions/search\u help.html。当从模板视图访问template\u name时,它返回“questions/paginated\u all\u questions.html” 为了使TemplateView.template_name等于从get_template_name()返回的值,可以做些什么?在本例中,templ

我试图根据QueryDict中包含或不包含的内容为TemplateView建立模板。当
get\u template\u names()
为真时,它将返回
questions/search\u help.html
。当从模板视图访问
template\u name
时,它返回
“questions/paginated\u all\u questions.html”

为了使
TemplateView.template_name
等于从
get_template_name()
返回的值,可以做些什么?在本例中,
template\u name==“questions/search.html”

我设置了
template\u name=None
,希望它能覆盖
AllQuestionsPage
视图的父值,并呈现
questions/search\u help.html
,但它没有效果

class SearchPage(AllQuestionsPage):
    template_name = None

    def get_template_names(self):
        if 'q' not in self.request.GET or self.request.GET['q'] == "":
            template_name = "questions/search_help.html"
        else:
            template_name = "questions/paginated_all_questions.html"
        return [template_name]


    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        if context['view'].template_name == 'questions/search_help.html':
            context['page_title'] = "Search"
        else:
            context['lookup_buttons'] = {
                'relevence': False,
                'newest': False
            }
            context['page_title'] = "Search Results"
        return context


    def get(self, request):
        context = self.get_context_data()
        if 'q' not in request.GET or request.GET['q'] == "":
            pass
        else:
            tab = request.GET.get('tab', 'relevence')
            q = request.GET.get('q', None)
            if not q:
                return HttpResponse(reverse('search'))
            if tab not in context['lookup_buttons'].keys() or tab == 'relevence':
                context['lookup_buttons'].update(relevence=True)
            else:
                context['lookup_buttons'].update(newest=True)
            regex1 = re.compile(r'[user|answers|score]+:\d+')
            search_match = re.match(regex1, q)
            regex = re.compile(r'\s*[?[a-z]+(?=])\s*|^\s*[a-z]+\s*$', re.I)
            tag_match = re.match(regex, q)
            if search_match:
                q_param, q_value = q.split(":")
                if q_param == 'user':
                    user = get_object_or_404(User, pk=int(q_value))
                    context['questions'] = Question.objects.filter(
                        user_account=user.user_account
                    )
                elif q_param == 'score':
                    context['questions'] = Question.objects.filter(
                        vote_tally__gte=int(q_value)
                    )
                else:
                    value = int(q_value)
                    context['questions'] = Question.objects.annotate(
                        total_answers=Count('answers')).filter(
                            total_answers__gte=value
                        )

            elif tag_match:
                q = tag_match.group().strip('[] ')
                tag = Tag.objects.filter(name=q)
                if tag:
                    return HttpResponseRedirect(
                        reverse("questions:tagged_search", kwargs={'tag': tag[0]})
                    )
                context['questions'] = Question.objects.filter(
                    Q(title__icontains=q)|Q(body__icontains=q)
                )
            else:
                context['questions'] = Question.objects.filter(
                    Q(title__icontains=q)|Q(body__icontains=q)
                )

            paginator = Paginator(context['questions'], 5)
            page_num = request.GET.get("page")
            context['page'] = paginator.get_page(page_num)
        return self.render_to_response(context)

> c:\users\..\questions\views.py(283)get_context_data()             
-> context = super().get_context_data(*args, **kwargs)                                                                                                                      
(Pdb) n                                                                                                                                                                     
> c:\users\..\questions\views.py(284)get_context_data()             
-> if context['view'].template_name == 'questions/search_help.html':                                                                                                        
(Pdb) n                                                                                                                                                                     
> c:\..\questions\views.py(288)get_context_data()             
-> 'relevence': False,                                                                                                                                                      
(Pdb) n                                                                                                                                                                     
> c:\..\questions\views.py(289)get_context_data()             
-> 'newest': False                                                                                                                                                          
(Pdb) n                                                                                                                                                                     
> c:\..\questions\views.py(291)get_context_data()             
-> context['page_title'] = "Search Results"                                                                                                                                 
(Pdb) n                                                                                                                                                                     
> c:\..\questions\views.py(292)get_context_data()             
-> return context                                                                                                                                                           
(Pdb) context['view'].template_name                                                                                                                                         
'questions/paginated_all_questions.html'                                                                                                                                    
(Pdb) self.get_template_names()                                                                                                                                             
['questions/search_help.html']                                                                                                                                              
(Pdb)