Answer the question
In order to leave comments, you need to log in
Why can't access the parent variable in the class?
Good afternoon, for example, we have such a CBV
class Post_Detail(TemplateView):
template_name = 'blog/post_detail.html'
class Post_Detail(TemplateView):
template_name = super().template_name
Answer the question
In order to leave comments, you need to log in
There is a get_template_names method where you can get the current template_name by self.template_name. Override this method.
Example:
class CategoryView(TemplateView):
template_folder = 'build'
template_name = 'build/category.html'
...
def get_template_names(self):
template = self.template_name
category_template = '{folder}/{slug}/category.html'.format(folder=self.template_folder, slug=self.category.slug)
return [category_template, template]
...
1) TemplateView doesn't have template_name variable defined
2) Do it in a method.
class A(object):
x = 5
class B(A):
x = 6
def foo(self):
return super().x # вернёт 5
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question