简体   繁体   中英

Create multiple website templates with django cms or any tool

Can Django be used to create a website for creating custom websites depending on the user needs? There will be more than one template and each user will have a custom url for their websites.

I don't know if this can this be achieved with Django CMS.

Sure, it is possible but it requires ton of work, because you need to write bunch of templates, or/and logic that handles templates.

You could write TemplateMixin , that depending on passed domain, or kwarg in url changes the template in the View . So, for example, it could look like that:

class TemplateMixin:
    template_name = None
    request = None
    model = None

    def get_template_names(self):
        """
        Return a list of template names to be used for the request.

         Must return a list. May not be called if render_to_response() is overridden.
        """
        db_settings = Settings.objects.get_or_create()[0]
        if self.template_name is None:
            raise ImproperlyConfigured(
                'TemplateResponseMixin requires either a definition of '
                '\"template_name\" or an implementation of \"get_template_names()\"')
        template = db_settings.website_template
        ready_template = '/'.join([template, self.template_name])
        return [ready_template]

There are also solutions for handling multiple domains, so you could detect what current domain is via self.request in get_template_names method. The templates are handled by prefix. The kwargs also should be in self.kwargs , so you can detect which site you need to present via custom namespace url.

You can also create WebsiteSettings model that handles various templates, and is manageable via simple custom dashboard panel, and belongs to user, or user groups. The fields that it should have is 'template' with choices attribute, relation to User (or CustomUser model), and bunch of settings you want to give a user (or you could have different model('s) for that).

The solution I bring here is simple to show the idea - but it can be insufficient, to accomplish what you really want you must write the whole mixins, views etc. logic yourself.

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