Wagtail multi-site: share content between sites

228 views
Skip to first unread message

Luiz Boaretto

unread,
Sep 4, 2016, 10:27:01 AM9/4/16
to Wagtail support
Hello,

I'm on a new project that is this way:

Default:
 - domain.com (Homepage like City chooser)

City A:
 - cityA.domain.com (Homepage City A)

City B:
 - cityB.domain.com (Homepage City B)

Category "sports" and "technology" are shared between the two sites. Category "politics" specific to each city.

It's possible that share the same content on both sites?

Many thanks,
Luiz

Matthew Westcott

unread,
Sep 12, 2016, 1:27:00 PM9/12/16
to wag...@googlegroups.com
Hi Luiz,
It's certainly possible. The only difficult part is that a page can only exist in one place within the tree, so you'll need to store the shared articles in some central location, and add some custom logic to make an article visible on multiple URLs across different sites, and to generate index pages (since you can't simply list all children of the current page).

RoutablePageMixin <http://docs.wagtail.io/en/v1.6.2/reference/contrib/routablepage.html> is a good fit for this kind of custom logic. Here's how I'd approach it:

* Define Snippet-registered models for 'City' and 'Category':

class City(models.Model):
name = models.CharField(max_length=255)

class Category(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255)
is_city_specific = models.BooleanField()

* Use wagtail.contrib.settings <http://docs.wagtail.io/en/v1.6.2/reference/contrib/settings.html> to associate each site with a City, via a CitySettings class:

@register_setting
class CitySettings(BaseSetting):
city = models.ForeignKey('myapp.City', null=True, on_delete=models.SET_NULL)

* Place SnippetChooserPanels for City and Category on the Article page model:

class Article(Page):
city = models.ForeignKey('myapp.City', null=True, on_delete=models.SET_NULL)
category = models.ForeignKey('myapp.Category', null=True, on_delete=models.SET_NULL)

promote_panels = Page.promote_panels + [
SnippetChooserPanel('city'),
SnippetChooserPanel('category'),
]

* Define a NewsIndex page, based on RoutablePageMixin, that handles both the per-category indexes and displaying individual articles; create one of these under each site, with the slug 'news'

class NewsIndex(RoutablePageMixin, Page):
@route(r'^$')
def news_index(self, request):
# Main news index, accessible at cityA.domain.com/news/
return render(request, 'myapp/news_index.html')

@route(r'^(\w+)/$')
def category_index(self, request, category_slug):
category = get_object_or_404(Category, slug=category_slug)
articles = Article.objects.filter(category=category)
if category.is_city_specific:
city = CitySettings.for_site(request.site).city
articles = articles.filter(city=city)

return render(request, 'myapp/category_index.html', {
'category': category, 'articles': articles,
})

@route(r'^(\w+)/(\w+)/$')
def show_article(self, request, category_slug, article_slug):
article = get_object_or_404(Article, slug=article_slug, category__slug=category_slug)
return render(request, 'myapp/show_article.html', {
'article': article
})


Cheers,
- Matt
Reply all
Reply to author
Forward
0 new messages