Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有多个URL的站点地图和对象_Python_Django_Sitemap - Fatal编程技术网

Python 具有多个URL的站点地图和对象

Python 具有多个URL的站点地图和对象,python,django,sitemap,Python,Django,Sitemap,Django中使用站点地图的正常方式是: from django.contrib.sitemaps import Sitemap from schools.models import School class SchoolSitemap(Sitemap): changefreq = "weekly" priority = 0.6 def items(self): return School.objects.filter(status = 2) 然后

Django中使用站点地图的正常方式是:

from django.contrib.sitemaps import Sitemap
from schools.models import School


class SchoolSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.6

    def items(self):
        return School.objects.filter(status = 2)
然后在学校模型中,我们定义:

  def get_absolute_url(self):
      return reverse('schools:school_about', kwargs={'school_id': self.pk})
在这样的实现中,我在sitemap.xml中有一个关于一所学校的链接

问题是我的学校有多个页面:关于、老师、学生和其他人,我希望所有的页面都是sitemap.xml


做这件事的最佳方法是什么?

您可以考虑以下事实:

如果字段的数量和名称不是预先确定的,我会选择一种完全动态的方法:允许模型使用
get\u sitemap\u url
方法返回绝对URL列表,并使用执行此方法的
sitemap
。也就是说,在最简单的情况下,您不需要访问priority/changefreq/lastmod方法中的对象:

class SchoolSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.6

    def items(self):
        return list(
             itertools.chain.from_iterable(( object.get_sitemap_urls()
                                             for object in 
                                             School.objects.filter(status = 2)))
        )

    def location(self, item):
        return item

非常感谢。您的解决方案正在运行,但我已将其更改为适合我的项目,因为每个模型对象的字段数可变。很高兴听到。我将修改答案,我将如何解决可变链接数的情况。再次感谢你!我用与对象函数和法线循环相同的方法制作它。你的方法看起来更优雅。
class SchoolSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.6

    def items(self):
        return list(
             itertools.chain.from_iterable(( object.get_sitemap_urls()
                                             for object in 
                                             School.objects.filter(status = 2)))
        )

    def location(self, item):
        return item