本文整理汇总了Python中django.contrib.sitemaps.GenericSitemap.get_urls方法的典型用法代码示例。如果您正苦于以下问题:Python GenericSitemap.get_urls方法的具体用法?Python GenericSitemap.get_urls怎么用?Python GenericSitemap.get_urls使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.sitemaps.GenericSitemap
的用法示例。
在下文中一共展示了GenericSitemap.get_urls方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_sitemaps
# 需要导入模块: from django.contrib.sitemaps import GenericSitemap [as 别名]
# 或者: from django.contrib.sitemaps.GenericSitemap import get_urls [as 别名]
def build_sitemaps():
sitemap_element = "<sitemap><loc>%s</loc><lastmod>%s</lastmod></sitemap>"
sitemap_index = "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"
for locale in settings.MDN_LANGUAGES:
queryset = (Document.objects
.filter(is_template=False, locale=locale)
.exclude(title__startswith='User:')
.exclude(title__iregex=r'Redirect [0-9]+$')
.exclude(html__iregex=r'^(<p>)?(#)?REDIRECT')
.exclude(slug__icontains='Talk:')
)
if len(queryset) > 0:
info = {'queryset': queryset, 'date_field': 'modified'}
sitemap = GenericSitemap(info, priority=0.5)
urls = sitemap.get_urls(page=1)
xml = smart_str(loader.render_to_string('sitemap.xml',
{'urlset': urls}))
xml = xml.replace('http://', 'https://')
directory = '%s/sitemaps/%s' % (settings.MEDIA_ROOT, locale)
if not os.path.exists(directory):
os.makedirs(directory)
f = open('%s/sitemap.xml' % directory, 'w')
f.write(xml)
f.close()
sitemap_url = ("https://%s/sitemaps/%s/sitemap.xml" % (
Site.objects.get_current().domain, locale))
sitemap_index = sitemap_index + sitemap_element % (sitemap_url,
time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime()))
sitemap_index = sitemap_index + "</sitemapindex>"
index_file = open('%s/sitemap.xml' % settings.MEDIA_ROOT, 'w')
index_file.write(parseString(sitemap_index).toxml())
index_file.close()
示例2: test_sitemap_item
# 需要导入模块: from django.contrib.sitemaps import GenericSitemap [as 别名]
# 或者: from django.contrib.sitemaps.GenericSitemap import get_urls [as 别名]
def test_sitemap_item(self):
"""
Check to make sure that the raw item is included with each
Sitemap.get_url() url result.
"""
user_sitemap = GenericSitemap({'queryset': User.objects.all()})
def is_user(url):
return isinstance(url['item'], User)
item_in_url_info = all(map(is_user, user_sitemap.get_urls()))
self.assertTrue(item_in_url_info)
示例3: test_sitemap_item
# 需要导入模块: from django.contrib.sitemaps import GenericSitemap [as 别名]
# 或者: from django.contrib.sitemaps.GenericSitemap import get_urls [as 别名]
def test_sitemap_item(self):
"""
Check to make sure that the raw item is included with each
Sitemap.get_url() url result.
"""
test_sitemap = GenericSitemap({'queryset': TestModel.objects.order_by('pk').all()})
def is_testmodel(url):
return isinstance(url['item'], TestModel)
item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls()))
self.assertTrue(item_in_url_info)
示例4: generate
# 需要导入模块: from django.contrib.sitemaps import GenericSitemap [as 别名]
# 或者: from django.contrib.sitemaps.GenericSitemap import get_urls [as 别名]
def generate():
sitemap = GenericSitemap({'queryset': models.Post.objects.filter(type__in=const.POST_TOPLEVEL).exclude(type=const.POST_BLOG), })
urlset = sitemap.get_urls()
text = loader.render_to_string('sitemap.xml', {'urlset': urlset})
text = smart_str(text)
site = Site.objects.get_current()
fname = path(settings.EXPORT_DIR, 'sitemap.xml')
print '*** writing sitemap for %s to %s' % (site, fname)
fp = open(fname, 'wt')
fp.write(text)
fp.close()
print '*** done'
示例5: generate_sitemap
# 需要导入模块: from django.contrib.sitemaps import GenericSitemap [as 别名]
# 或者: from django.contrib.sitemaps.GenericSitemap import get_urls [as 别名]
def generate_sitemap():
sitemap = GenericSitemap({
'queryset': Post.objects.filter(type__in=Post.TOP_LEVEL).exclude(type=Post.BLOG),
})
urlset = sitemap.get_urls()
text = loader.render_to_string('sitemap.xml', {'urlset': urlset})
text = smart_str(text)
site = Site.objects.get_current()
fname = path(settings.STATIC_ROOT, 'sitemap.xml')
logger.info('*** writing sitemap for %s to %s' % (site, fname))
fp = open(fname, 'wt')
fp.write(text)
fp.close()
logger.info('*** done')