views:

547

answers:

2

Hi!

I have following models relation:

class Section(models.Model):
    section = models.CharField(max_length=200, unique=True)
    name = models.CharField(max_length=200, blank = True)


class Article (models.Model):
    url = models.CharField(max_length = 30, unique=True)
    is_published = models.BooleanField()  
    section = models.ForeignKey(Section)

I need to create a sitemap for articles, which contains sitemap files for sections. I was reading django documentation about it here http://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/

But didn't manage to find answer how can I:

  1. Define sitemap class in this case
  2. How can I pass section parameters into url file (as it's explained in the docs)
  3. From where can I get {'sitemaps': sitemaps} if I defined sitemap as a python class in another file in the application
A: 

Your question is not at all clear.

  1. Define it as described in the docs. What are you not clear about?
  2. What parameters? What url file - do you mean urls.py? This has nothing to do with a sitemap.
  3. You import it, like you would with any other code that you define in a different file.
Daniel Roseman
But, to create a section sitemap I need to implement filter.Something like this:Acrticle.objects.filter(section = Section.objects.get(section = section))Not sure where should I implement the filter
Oleg Tarasenko
+2  A: 

If i understand correctly you want to use sitemap index that would point at seperate sitemap xml files each for every section.

Django supports this feature by providing a separate sitemap view for index sitemaps.

Haven't used that feature before but something like the following would probably work in your case.

### sitemaps.py
from django.contrib.sitemaps import GenericSitemap
from models import Section

all_sitemaps = {}
for section in Section.objects.all():

    info_dict = {
        'queryset': section.article_set.filter(is_published=True),
    }

    sitemap = GenericSitemap(info_dict,priority=0.6)

    # dict key is provided as 'section' in sitemap index view
    all_sitemaps[section.name] = sitemap

### urls.py
from sitemaps import all_sitemaps as sitemaps

...
...
...

urlpatterns += patterns('',
        (r'^sitemap.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}),
        (r'^sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
)
vinilios