简体   繁体   中英

django choose template from admin panel without restarting server

I'm trying to create a website with multiple template for all pages.

I've created a templates folder and there are 3 folders in it. each folder contains base.html , home.html , etc .

Admin can choose each template from admin panel and in my view load template like this.

class HomeView(TemplateView):
    default_template = CustomTemplate.objects.first().name
    template_name = default_template + '/home.html'

The problem is I have to restart server to apply admin's changes . Is there any way to do this without restarting server? I've also tried to enable / disable loader cache but I guess the problem is not depends to cache system.

Anything defined directly at class level will persist for the entirety of a process.

Luckily, Django's class-based views provide a series of hooks so that you can define things on a per-request basis. In this case, the method you want is get_template_names (which returns a list of templates to search for).

So:

class HomeView(TemplateView):
    def get_template_names(self):
        default_template = CustomTemplate.objects.first().name
        return ['{}/home.html'.format(default_template)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM