Django: accessing variable value from TemplateView
<p>Say I have the following url that maps to a <code>TemplateView</code>:</p>

<pre><code>url(r'^path/(?P&lt;var1&gt;\d+)/(?P&lt;var2&gt;\d+)/$', TemplateView.as_view('a_view.html'))
</code></pre>

<p>I thought in the template view <code>a_view.html</code> I could access the values of <code>var1</code> and <code>var2</code> as they're being captured and extracted into named parameters:</p>

<pre><code>&lt;!-- a_view.html --&gt;

&lt;p&gt;var1 value = {{ var1 }}&lt;/p&gt;
&lt;p&gt;var2 value = {{ var2 }}&lt;/p&gt;
</code></pre>

<p>However, these values are blank when visiting <code>/path/10/89</code>. Why? How can I access them then? Would I need an explicit view?</p>

<p>I think something like this should work.</p>

<p>Change you urls.py to use a named view:</p>

<pre><code>url(r'^path/(?P&lt;var1&gt;\d+)/(?P&lt;var2&gt;\d+)/$', YourNamedView.as_view('a_view.html'))
</code></pre>

<p>Create a TemplateView and let it grab your vars and add it to the context:</p>

<pre><code>class YourNamedView(TemplateView):
    template_name = 'a_view.html'

    def get_context_data(self, **kwargs):
        context = super(YourNamedView, self).get_context_data(**kwargs)
        context.update({
            'var1': self.kwargs.get('var1', None),
            'var2': self.kwargs.get('var2', None),
        })
        return context
</code></pre>

<p>and in the template:</p>

<pre><code>&lt;h1&gt;{{ var1 }} {{ var2 }}&lt;/h1&gt;
</code></pre>

python django