简体   繁体   English

Django在同一个实例上的多个站点

[英]Django multiple sites on same instance

How can I get a few sites similar in functionality, but different in appearance and content - all data will be different. 我怎样才能获得一些功能相似但外观和内容不同的网站 - 所有数据都会有所不同。

Change SITE_ID on the fly and write own template loader & staticfile finder (same URL /static/ for different sites indicates to different categories) and add ForeignKey to Site for all? 动态更改SITE_ID并编写自己的模板加载器和静态文件查找器(相同的URL /静态/不同的站点指示不同的类别)并为所有人添加ForeignKey到站点?

or are there other ways to? 或者还有其他方法吗?

Thanks! 谢谢!

I did something similar to yourself. 我做了类似于你自己的事情。

I have a helper method in my views which gets the host. 我的视图中有一个帮助方法来获取主机。

def getHost(request): return request.META['SERVER_NAME'] def getHost(request):return request.META ['SERVER_NAME']

Then depending on the host i have my static dir broken into the 2 sites static/host1/ static/host2/ 然后根据主机我的静态目录分为2个站点static / host1 / static / host2 /

and i do the same for templates i split it into 2 directories and call the appropriote one! 我为模板做同样的事情我把它分成2个目录并调用appropriote!

It is a delicate operation to split a Django site on the same instance. 在同一个实例上拆分Django站点是一项微妙的操作。 First, you have to have your models in mind. 首先,您必须牢记您的模型。 Like FlatPages do, your models may contain a ManyToManyField to the Sites table so you can filter content according to the current site if you want. 与FlatPages一样,您的模型可能包含到Sites表的ManyToManyField,因此您可以根据需要根据当前站点过滤内容。

About templates and static data, I like Mark's answer but you can do it differently though. 关于模板和静态数据,我喜欢Mark的答案,但你可以采用不同的方式。 You could split your template and static folders into several folders with different themes and template configurations. 您可以将模板和静态文件夹拆分为具有不同主题和模板配置的多个文件夹。 For example, you could have only one set of templates, and two "themes" like this : 例如,您可以只有一组模板,以及两个“主题”,如下所示:

templates 
    / normal_set
static 
    / themes 
        / blue
        / red

You could create a table calles SiteConfiguration, with a OneToOneField to the Sites table. 您可以使用OneToOneField创建一个表calles SiteConfiguration到Sites表。 This configuration table would contain the name of the template folder and the name of the theme folder for the site so you can share properties between sites. 此配置表将包含模板文件夹的名称和站点的主题文件夹的名称,以便您可以在站点之间共享属性。 Maybe you can cache this afterwards to avoid hitting the database too often. 也许你可以在之后缓存它以避免过于频繁地访问数据库。 Mix this suggestion with Mark's one, maybe. 也许,将这个建议与马克的建议结合起来。

I assume your users remain the same through every site. 我假设您的用户在每个站点都保持不变。

There are 2 packages designed for this exact use case: 为这个确切的用例设计了2个包:

for Django 1.8+, there is https://bitbucket.org/levit_scs/django-polla 对于Django 1.8+,有https://bitbucket.org/levit_scs/django-polla

If you are using an older version of Django, you can use https://bitbucket.org/uysrc/django-dynamicsites/overview 如果您使用的是旧版本的Django,可以使用https://bitbucket.org/uysrc/django-dynamicsites/overview

Even though this answer is quite late, I do hope it can help people stumbling on this question 即使这个答案很晚,但我希望它可以帮助人们绊倒这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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