5

我有一个 Web 应用程序,我想允许为我们的客户贴上白标。我已经在 PHP/ZendFramework 中完成了这项工作,关闭主机名 ( http://example.com ),从数据库中提取 logo/colors/other 并使用这些设置呈现主布局。

我是 Python/Django1.5 的新手,想知道是否有人在他们的应用程序中实现了白标功能。你是怎么做到的?有没有普遍的做法?

我做了一些谷歌搜索,发现一个较旧的博客使用 url 前缀实现了白标签功能,但在渲染布局时我仍然遇到一些问题

http://chase-seibert.github.com/blog/2011/08/05/django-white-label-styling-with-url-prefixes.html

任何帮助都会很棒!谢谢

4

1 回答 1

10

我没有找到一个好的答案,所以我只是实现了我自己的解决方案。

我所做的是创建一个Whitelabel看起来像这样的模型:

class Whitelabel(models.Model):
    name = models.CharField(max_length=255, null=False)
    logo = models.CharField(max_length=255, null=True, blank=True)
    primary_domain = models.CharField(max_length=256, null=False) 

然后我创建了一个上下文处理器,application_name/context_processors.py用于检查当前主机域并查看它是否与任何记录primary_domain字段匹配。如果匹配,则返回 and 的值name并将logo它们分配给参数SITE_NAMEand SITE_LOGO。如果未找到匹配项,请为SITE_NAME和分配默认值SITE_LOGO,这可能是您的默认应用程序名称。

def whitelabel_processor(request):
    current_domain = request.get_host() 
    whitelabel = Whitelabel.objects.filter(primary_domain=current_domain).order_by('id')

    if whitelabel.count() != 0:
        config = {
            'SITE_NAME': whitelabel[0].name, 
            'SITE_LOGO': whitelabel[0].logo, 
            'SITE_DOMAIN': whitelabel[0].primary_domain
            }
    else:
        config = {
            'SITE_NAME': 'MY SITE', 
            'SITE_LOGO': '/static/images/logo.png', 
            'SITE_DOMAIN': 'http://%s' % Site.objects.get_current().domain
            }

    return config

然后我将上下文处理器添加到我的设置文件中TEMPLATE_CONTEXT_PROCESSORS

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    ...
    "context_processors.whitelabel_processor",

)

这样我就可以在我的base.html模板中这样称呼它们

<body>
    <h1>{{SITE_NAME}}</h1>
    <img src="{{SITE_LOGO}}" />
</body>

这是有关模板上下文处理器的更多文档。 https://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors

于 2013-05-07T21:39:41.167 回答