If I have a class based view, like this,
class SomeView (View): response_template='some_template.html' var1 = 0 var2 = 1 def get(self, request, *args, **kwargs): return render_to_response(self.response_template, locals(), context_instance=RequestContext(request))
My question is, inside the template some_template.html
, how do I access var1
and var2
? As far as I understood this, the locals()
sort of just dumps all the local variables into the template, which has worked very well so far. But these other variables aren't technically "local", they're part of a class, so how do I pass them over??
Thanks!
And this is rather simple, because Django has built-in template modules that makes a transfer easy. Basically you just take the variable from views.py and enclose it within curly braces {{ }} in the template file.
Passing context into your templates from class-based views is easy once you know what to look out for. There are two ways to do it – one involves get_context_data, the other is by modifying the extra_context variable.
A view is a callable which takes a request and returns a response. This can be more than just a function, and Django provides an example of some classes which can be used as views. These allow you to structure your views and reuse code by harnessing inheritance and mixins.
Generic class-based views are a great choice to perform all these tasks. It speeds up the development process. Django provides a set of views, mixins, and generic class-based views. Taking the advantage of it you can solve the most common tasks in web development.
A cleaner way of doing this could be to replicate Django's Template view:
class TemplateView(TemplateResponseMixin, ContextMixin, View): """ A view that renders a template. This view will also pass into the context any keyword arguments passed by the url conf. """ def get(self, request, *args, **kwargs): context = self.get_context_data(**kwargs) return self.render_to_response(context)
and then adding it to the get_context_data function. Or you could simply use the TemplateView which will allow you to specify a template name and then you could override the get_context_data function:
class SomeView(generic.TemplateView): var1 = 0 var2 = 1 template_name = 'some_template.html' def get_context_data(self, **kwargs): context = super(SomeView, self).get_context_data(**kwargs) context.update({'var1': self.var1, 'var2': self.var2}) return context
EDIT
Django has generic views which you can use for a variety of things, I would strongly advise you to go look at the docs for a full list of them, These generic views have functions you can override to do custom things which aren't supported by default. In your case you just wanted a template with context variables on them which means you subclass the TemplateView and supply the template_name and then finally you can override the get_context_data function to add your context data and that would be all there is to it, the second piece of code would be all you need in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With