A
A
Andrey2016-03-23 10:00:24
Django
Andrey, 2016-03-23 10:00:24

How to change TEMPLATE_DIRS?

Good afternoon!
I want to gradually move the site to a new design!
It is necessary that TEMPLATE_DIRS be changed to a different path if the request contains site.ru/?new_style=1
Tell me how to do this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Aksenov, 2016-03-25
@andrey_u

For this, you can add middleware.py with similar content

# -*- coding: UTF-8 -*-
from django.conf import settings


class NewStyleTemplateDir(object):

    @staticmethod
    def process_request(request):
        if request.GET.get('new_style'):
            settings.TEMPLATES[0]['DIRS'] = ['новая директория с шаблонами']

And add this middleware to settings.py in MIDDLEWARE_CLASSES

N
newpy, 2016-03-23
@newpy

maybe something like this?

import settings
if 'new_style' in request.GET:
    style = request.GET.get('new_style')
    делаем что-то с settings.TEMPLATE_DIRS ( = style directory)

O
Oscar Django, 2016-03-23
@winordie

When using cbv descendants of TemplateResponseMixin, the get_template_names method is responsible for this.
You can set TEMPLATES['DIRS'] to both folders: old and new, give new templates slightly different names (with _ at the beginning for example) and place them in the new folder. And then write:

class MyView(TemlateView):
  def get_template_names(self):
    style = self.request.GET.get('new_style', 0)
    if style == 1:
      return list(map(lambda t: '_{}'.format(t), super().get_template_names())
    return super().get_template_names()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question