Template View - kwargs and **kwargs
<p>I am reading about Template views through a tutorial and some of the code kind of confused me. The author used this code sample</p>

<pre><code>from django.utils.timezone import now

class AboutUsView(TemplateView):
    template_name = 'about_us.html'

def get_context_data(self, **kwargs):
    context = super(AboutUsView, self).get_context_data(**kwargs)
    if now().weekday() &lt; 5 and 8 &lt; now().hour &lt; 18:
        context['open'] = True
    else:
        context['open'] = False
    return context
</code></pre>

<p>The thing that confused me syntactically was this statement</p>

<pre><code> context = super(AboutUsView, self).get_context_data(**kwargs)
</code></pre>

<p>if we already are receiving <code>**kwargs</code> then why are we passing it to the super function with ** (double start). I think we should pass it as </p>

<pre><code> context = super(AboutUsView, self).get_context_data(kwargs)
</code></pre>

<p>this is the contextMixin which is receiving this call.</p>

<pre><code>class ContextMixin(object):
    """
    A default context mixin that passes the keyword arguments received by
    get_context_data as the template context.
    """

    def get_context_data(self, **kwargs):
        if 'view' not in kwargs:
            kwargs['view'] = self
        return kwargs
</code></pre>

<p>From what I have read is that the use of <code>**kwargs</code> pretty much means that kwargs is currently a dictionary and needs to be converted to named-value. If that is correct then how can kwargs be a dictionary when its parameter is actually **kwargs. I hope my question makes sense. Please let me know if you would want me to rephrase this.</p>

<p>In a function declaration, <code>**kwargs</code> will take all unspecified keyword arguments and convert them into a dictionary.</p>

<pre><code>&gt;&gt;&gt; test_dict = {'a':1, 'b':2}
&gt;&gt;&gt; def test(**kwargs):
...     print (kwargs)
...
&gt;&gt;&gt; test(**test_dict)
{'b': 2, 'a': 1}
</code></pre>

<p>Note that the dictionary object has to be converted using <code>**</code> when it is passed to the function (<code>test(**test_dict)</code>) and when it is received by the function. It is impossible to do the following:</p>

<pre><code>&gt;&gt;&gt; test(test_dict)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: test() takes 0 positional arguments but 1 was given
</code></pre>

<p>So, in your example, the first <code>**kwargs</code> unpacks the keyword arguments into a dictionary, and then the second packs them back up to be sent to the parent.</p>

<p>A function with <code>**kwargs</code> in the signature can either received an unpacked dictionary or unspecified keyword arguments. Here's an example of the second case:</p>

<pre><code>&gt;&gt;&gt; def test(arg1, **kwargs):
...     print (kwargs)
...
&gt;&gt;&gt; test('first', a=1, b=2)
{'b': 2, 'a': 1}
</code></pre>

python django