Concisely adding variables to Django context dictionary
<p>The Django documentation suggests adding variables to the context dictionary in class based views as follows:</p>

<pre><code>def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(PublisherDetail, self).get_context_data(**kwargs)
        # Add in a QuerySet of all the books
        context['book_list'] = Book.objects.all()
        return context
</code></pre>

<p>I often have a lot of variables in my gather context function, so my code ends up looking like this:</p>

<pre><code>def get_context_data(self, **kwargs):
        context = super(PublisherDetail, self).get_context_data(**kwargs)
        a = 2
        b = 'hello'
        c = 75 # temp variable, not wanted in context dict
        d = a * c

        context['a'] = a
        context['b'] = b
        context['d'] = d
        return context
</code></pre>

<p>To avoid adding each variable to the context dictionary on its own line, I've begun doing the following:</p>

<pre><code>def get_context_data(self, **kwargs):
        context = super(PublisherDetail, self).get_context_data(**kwargs)
        a = 2
        b = 'hello'
        c = 75 # temp variable, not wanted in context dict
        d = a * c

        del c # delete any variables you don't want to add
        context.update({k:v for k,v in locals().copy().iteritems() if k[:2] != '__' and k != 'context'})
        return context
</code></pre>

<p>It seems to be working, but I'm not very familiar with locals(). Is there any reason why I shouldn't be doing it this way?</p>