title: Django changing template_name in get_context_data dynamically
body: <p>how do I override the <code>template_name</code> of a <code>TemplateView</code> based on the context in <code>get_context_data</code>? In other words, I would like to find a way to change the template rendered according to my context.</p>
  <p>The following is modified from my current code and it does not work:</p>
  <pre><code>class FooPage(TemplateView):
    template_name = &quot;folder/template1.html&quot;
    
    def get_context_data(self, **kwargs):
      context = super().get_context_data(**kwargs)
      context['myvariable'] = MyModel.objects.get(pk=kwargs['some_criterion']).variable
      if &quot;text&quot; in context['myvariable']:
        template_name = &quot;folder/template2.html&quot; # This line does get executed
      return context
  </code></pre>
  <p>I can confirm that the line changing <code>template_name</code> is executed and it is indeed set to <code>&quot;folder/template2.html&quot;</code> at that point, but the actual page that gets rendered still looks like the original (<code>template1.html</code>) instead.</p>
  <p>Any helpful insight would be deeply appreciated. Thank you very much!</p>