Django 1.6: What should be the location of the sitemap.py file?

919 Views Asked by At

I want to implement django sitemaps but I'm little confused about where to put the sitemaps.py file and which urls.py file should i modify to include:

url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}),

Shall I put the above line in project(mysite) urls.py file or the app(sampleapp1) urls.py file?

2

There are 2 best solutions below

0
On BEST ANSWER

You create sitemaps within the apps of your project (the ones you need sitemaps), documentation doesn't state what and where because you are free to do as you like, you only need to register them in a dictionary which is passed in the url. For example, you have a your_project which has a blog app:

your_project
  - blog
    - models.py
    - views.py
    - ...
    - sitemap.py

In your sitemap.py:

from django.contrib.sitemaps import Sitemap
from blog.models import Entry

class BlogSitemap(Sitemap):
    changefreq = "never"
    priority = 0.5

    def items(self):
        return Entry.objects.filter(is_draft=False)

    def lastmod(self, obj):
        return obj.pub_date

Then in your urls.py (main urls.py of your project):

...

from blog.sitemap import BlogSitemap

sitemaps = {
    'blog':BlogSitemap
}


url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),

You repeat the same for every sitemap you need, if your project is big (many apps) it would be wise to use different sitemaps per app, then in your urls.py:

...

from blog.sitemap import BlogSitemap
from fooapp.sitemap import FooSitemap
from barapp.sitemap import BarSitemap

sitemaps = {
    'blog':BlogSitemap,
    'foo':FooSitemap,
    'bar':BarSitemap,
}
1
On

The documentation does highlight this but here is the summarized version:

  1. Make sure you have the correct settings. This boils down to two things:

    1. Have django.contrib.sitemaps in INSTALLED_APPS
    2. Make sure you have a site defined. It used to be that this was done by default, but from 1.6 it has become optional. However a lot of contrib apps and some third party apps rely on this to be in place.
  2. Next, you have to decide where to put your sitemap.xml. This is important because the location of this will control what is included in the site map. If you point this to /foo/sitemap.xml only those urls that start with foo/ will be included in the sitemap. So, its best to put it in the root url conf, which is typically at your project level. All you have to do is add the line in your question to the appropriate urls.py.

  3. Now you have to actually generate the sitemap. Django provides two automatic ways to generate this. GenericSiteMap for your models, and FlatPageSitemap for everything else. You can of course create your own custom sitemap.

    1. Create a class that inherits from django.contrib.sitemaps.SiteMap
    2. In this class, create a method items. This is what will have the links in the sitemap. For your objects, you can just return a queryset. If you want to create custom links, then return a list of url names, and django will automatically call reverse() to calculate the URL.
    3. Import this class name and pass it as the 'sitemap' dictionary in the root urls.py. So if you have ProductMap for all your products, then you would pass {'sitemaps': {'products': ProductMap}} as the last argument (after making sure you import it).

That's all there is to it.