3

I would like to create a Site specific Sitemap using Django Sitemaps.

My idea was to do something like this :

def items(self):
    current_site = Site.objects.get_current(self.request)
    return current_site.pages.filter(draft=False)

But I have two issues :

  1. self.request is not defined, is there a way to get the real current_site inside the Sitemap ?
  2. The items URL is not right, it should starts with the right current_site

I guess, I have to pass directly the right queryset to my Sitemap, but how can I do that ?

Thanks,

Rémy

Vivek S
  • 5,384
  • 8
  • 51
  • 72
Natim
  • 17,274
  • 23
  • 92
  • 150
  • Why you are passing `self.request` to `get_current` method? simply call `Site.objects.get_current()` this will give you current site! – Ahsan May 14 '12 at 06:48

2 Answers2

1

You can pass the site specific queryset by this way.

def items(self):   
  return YourModel.objects.by_site()

by_site is YourModel's manager function,

def by_site(self, site=None, **kwargs):
    """Get all pages for this site"""
    if not site:
        site = Site.objects.get_current()

    site = site.id

    return self.filter(site__id__exact = site, **kwargs)
Ahsan
  • 11,516
  • 12
  • 52
  • 79
  • 1
    Yes but in this case Site.objects.get_current() will return the settings.SITE_ID one instead of the RequestSite – Natim Jun 05 '12 at 08:29
-2

It sounds like the sites framework is being used the wrong way

The sites framework isn't intended to dynamically switch sites based on the request. It is for use with multiple separate deployments running from the same database.

Issue 1:

To fix your items() function, SITE_ID needs to be set in settings.py Which lets Site.objects.get_current() work anywhere, without needing a request object.

Issue 2:

Domain names should not be included in the urls. The sitemap is requested from a domain, so everything is known to be on that domain.

This goes for all urls within django.

To fix it, make sure the get_absolute_url() method on your model is returning a string starting with "/". Not anything relating to a domain name.

The url for an item is generated with get_absolute_url() or by calling Sitemap.location() with the item. The documentation for location() examples about what form the url should be in.

For more information check out this bug ticket, about the same issue.

Damien Ayers
  • 1,551
  • 10
  • 17
  • Actually, I am using `Site` the way I need it for my SaaS. But I think using the `SITE_ID` is just a simple way of using this contrib app. – Natim May 14 '12 at 11:16
  • `SITE_ID` is **the** way to use `contrib.sites`. The alternative `RequestSite`/`get_current_site(request)` is just a convenience fallback for when not using `contrib.sites`. If you want to use it this way you'll have to override some methods in the the `Sitemap` class, which may change and break in the future. – Damien Ayers May 14 '12 at 12:33
  • Ok so I can also fork the Sitemap app and create one which will worK. – Natim May 14 '12 at 15:37