django 1.6 Sitemap无法正常工作

时间:2014-09-21 15:48:52

标签: python django sitemap

我正在尝试为我的模型实现站点地图。但它只显示任何一种模型的链接。我必须在诊所发表评论,以显示医生和副医生的链接。

以下是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)])

它是否与Clinic是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)

使用上面的代码,它显示了静态页面和医生模型的站点地图链接。

1 个答案:

答案 0 :(得分:1)

您在两个站点地图中使用相同的dict,这就是您看到相同链接的原因。 GenericSitemap是一个使用给定字典的queryset属性的快捷方式。

但我也想知道为什么你没有使用扩展的Sitemap类?