encode / django-vanilla-views

Beautifully simple class-based views.
http://django-vanilla-views.org/
BSD 2-Clause "Simplified" License
985 stars 74 forks source link

Add view kwargs (e.g. from URL mapping) to rendering context #25

Closed qris closed 10 years ago

qris commented 10 years ago

I was wondering what happened to the kwargs passed to the get() method. So I checked TemplateView and found this:

class TemplateView(GenericView):
    def get(self, request, *args, **kwargs):
        context = self.get_context_data()
        return self.render_to_response(context)

I.e. the kwargs are completely ignored and discarded.

How about passing them to get_context_data instead? Then we can write our URLs with keyword argument placeholders:

url(r'^commitment/(?P<declaration>\S+)/(?P<commitment>\S+)/(?P<country>\S+)$',
    views.CommitmentComparison.as_view(), name='commitment-comparison'),

and those parameters will end up in our context, and hence available to the template, without any extra work.

tomchristie commented 10 years ago

They're not included in Django, so also following the same style and not including them here. (Keeping exactly the same functionality but without the ugly-ass hard to understand style.) You'll need to override get_context_date yourself if you want this (you can do that as a mixin or base class if you want to only write it once)