James L.
James L.

Reputation: 1153

django 1.6 Sitemap not working properly

I am trying to implement sitemap for my models. But it only shows the links for either model. I have to comment out the clinic to show the links for the doctor and vice-verca.

Here are the models.py

class Clinic(models.Model):
    name = models.CharField(max_length=500)
    slug = models.CharField(max_length=200, blank = True, null = True, unique = True)
    contact_no = models.IntegerField() 
    submitted_on = models.DateTimeField(auto_now_add=True, null = True, blank = True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Clinic, self).save(*args, **kwargs)

    def get_absolute_url(self):
        from django.core.urlresolvers import reverse
        return reverse('meddy1.views.clinicProfile', args=[str(self.slug)])

    def __unicode__(self):
        return u"%s %s" % (self.name, self.contact_no) 

class Doctor(models.Model):
    name = models.CharField(max_length=1300)
    title = models.CharField(max_length=1300, null = True, blank = True)
    specialization = models.ForeignKey(Specialization)
    clinic = models.ForeignKey(Clinic)
    education1 = models.CharField(max_length=1300)

    def __unicode__(self):
      return u"%s %s" % (self.name, self.specialization)

    def get_absolute_url(self):
        from django.core.urlresolvers import reverse
        return reverse('meddy1.views.showDocProfile', args=[str(self.id)])

Does it have anything to do with the fact that Clinic is a foreign key in the Doctor Model ?

urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView
from meddy1.models import *
from django.contrib.sitemaps import GenericSitemap

from django.contrib.sitemaps.views import sitemap

from meddy1.sitemap import *

from meddy1 import views


admin.autodiscover()


info_dict = {
    'queryset': Doctor.objects.all(),
    # 'queryset': Clinic.objects.all(),
    'date_field': 'submitted_on',
}

sitemaps = {
    'doctor': GenericSitemap(info_dict, priority=0.6),
    'static': StaticViewSitemap,
    'clinic': GenericSitemap(info_dict, priority=0.6),
}

urlpatterns = patterns('',
    url(r'^', include('meddy1.urls')),
    url(r'^', include('django.contrib.auth.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^robots\.txt$', TemplateView.as_view(template_name="meddy1/robots.txt")),
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),

sitemap.py

from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse

from meddy1.models import *

class DoctorSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.5

    def items(self):
        return Doctor.objects.all()

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


class ClinicSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.5

    def items(self):
        return Clinic.objects.all()

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

class StaticViewSitemap(Sitemap):
    priority = 0.5
    changefreq = 'weekly'

    def items(self):
        return ['index', 'about', 'privacy','terms', 'login', 'signup']

    def location(self, item):
        return reverse(item)

With the above code it shows the sitemap links for the static page and doctor model.

Upvotes: 0

Views: 208

Answers (1)

ACimander
ACimander

Reputation: 1979

You are using the same dict for both sitemaps, that's why you are seeing the same links. GenericSitemap is a shortcut which uses the queryset attribute of the given dict.

But I also wonder why you aren't using your extended Sitemap classes?

Upvotes: 1

Related Questions