Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL-parameters and logic in Django class-based views (TemplateView)

People also ask

What is TemplateView Django?

Django Templates are used to create HTML interfaces that get rendered with a Django view. A TemplateView is a generic class-based view that helps developers create a view for a specific template without re-inventing the wheel. TemplateView is the simplest one of many generic views provided by Django.

How do I redirect a class-based view in Django?

Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .


To access the url parameters in class based views, use self.args or self.kwargs so you would access it by doing self.kwargs['year']


In case you pass URL parameter like this:

http://<my_url>/?order_by=created

You can access it in class based view by using self.request.GET (its not presented in self.args nor in self.kwargs):

from django.views.generic.list import ListView

class MyClassBasedView(ListView):
    ...
    def get_queryset(self):
        order_by = self.request.GET.get('order_by') or '-created'
        qs = super().get_queryset()
        return qs.order_by(order_by)

I found this elegant solution, and for django 1.5 or higher, as pointed out here:

Django’s generic class based views now automatically include a view variable in the context. This variable points at your view object.

In your views.py:

from django.views.generic.base import TemplateView    

class Yearly(TemplateView):
    template_name = "calendars/yearly.html"
    # Not here 
    current_year = datetime.datetime.now().year
    current_month = datetime.datetime.now().month

    # dispatch is called when the class instance loads
    def dispatch(self, request, *args, **kwargs):
        self.year = kwargs.get('year', "any_default")

    # other code

    # needed to have an HttpResponse
    return super(Yearly, self).dispatch(request, *args, **kwargs)

The dispatch solution found in this question.
As the view is already passed within Template context, you don't really need to worry about it. In your template file yearly.html, it is possible to access those view attributes simply by:

{{ view.year }}
{{ view.current_year }}
{{ view.current_month }}

You can keep your urlconf as it is.

It's worth mentioning that getting information into your template’s context overwrites the get_context_data(), so it is somehow breaking the django's action bean flow.


So far I've only been able to access these url parameters from within the get_queryset method, although I've only tried it with a ListView not a TemplateView. I'll use the url param to create an attribute on the object instance, then use that attribute in get_context_data to populate the context:

class Yearly(TemplateView):
    template_name = "calendars/yearly.html"

    current_year = datetime.datetime.now().year
    current_month = datetime.datetime.now().month

    def get_queryset(self):
        self.year = self.kwargs['year']
        queryset = super(Yearly, self).get_queryset()
        return queryset

    def get_context_data(self, **kwargs):
        context = super(Yearly, self).get_context_data(**kwargs)
        context['current_year'] = self.current_year
        context['current_month'] = self.current_month
        context['year'] = self.year
        return context