Как я могу сделать шаблоны на основе условий в Django DetailView - PullRequest
1 голос
/ 14 мая 2019

Я попытался переопределить get_templates_name (). Но это не помогает мне.

def get_template_names(self):
    theme = Themes.objects.filter(theme_creator=self.request.user)
    for t in theme:
        if t.technology_theme == True:
            return ["landing/preview/preview1.html/"]       
        elif t.default_theme == True:
            return  ["landing/categories/technology/technology1.html/"]

Ответы [ 2 ]

1 голос
/ 14 мая 2019

Инициализируйте переменную наподобие

 if t.technology_theme == True:
     template_name = "landing/preview/preview1.html"
 elif t.default_theme == True:
     template_name = "landing/categories/technology/technology1.html"

и верните имя шаблона в виде:

В CBV:

template_name=template_name

В FBV:

retrun render(request, template_name, context)
0 голосов
/ 14 мая 2019

Внутри get_object () или get_context_data () вы можете определить имя шаблона следующим образом:

def get_context_data(self, **kwargs):
    context = super(YourView, self).get_context_data(**kwargs)
    self.template_name = 'your_template.html'
    return context
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...