金字塔:如何自动生成站点地图?

2024-06-25 23:52:50 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法可以动态生成站点地图并定期使用金字塔提交给谷歌?在

我看到了两个代码片段(herehere)在烧瓶中执行此操作,但它们似乎不适用于金字塔。在

具体来说,当我在__init__.py中包含config.add_route('sitemap', '/sitemap.xml'),然后是以下视图:

@view_config(route_name='sitemap', renderer='static/sitemap.xml')
def sitemap(request):
    ingredients = [ ingredient.name for ingredient in Cosmeceutical.get_all() ]
    products = [ product.name for product in Product.get_all() ]
    return dict(ingredients=ingredients, products=products)

我得到一个错误:

^{pr2}$

将视图更改为:

@view_config(route_name='sitemap', renderer='static/sitemap.xml.jinja2')
def sitemap(request):
    ingredients = [ ingredient.name for ingredient in Cosmeceutical.get_all() ]
    products = [ product.name for product in Product.get_all() ]
    request.response.content_type = 'text/xml'
    return dict(ingredients=ingredients, products=products)

从以前的位置越过KeyError,但当我尝试导航到mysite.com/static/sitemap.xml.发生了什么事时,给出了404?在

编辑:这是我的站点地图.jinja2文件。在

<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

{% for page in other_pages %}
<url>
  <loc>http://www.wisderm.com/{{page}}</loc>
  <changefreq>weekly</changefreq>
</url>
{% endfor %}

{% for ingredient in ingredients %}
<url>
  <loc>http://www.wisderm.com/ingredients/{{ingredient.replace(' ', '+')}}</loc>
  <changefreq>monthly</changefreq>
</url>
{% endfor %}

{% for product in products %}
<url>
  <loc>http://www.wisderm.com/products/{{product.replace(' ', '+')}}</loc>
  <changefreq>monthly</changefreq>
</url>
{% endfor %}

</urlset>

Tags: nameinhttpurlforgetwwwxml
2条回答

看看here

from pyramid.threadlocal import get_current_registry

def mview(request):
    reg = get_current_registy

{{source}在cd1>方法中查找。在

也许它可以帮助您“动态”生成站点地图:)

如果您想建立http://example.com/sitemap.xml作为站点地图的URL,请这样做。在

将这一行添加到init.py以注册URL模式http://example.com/sitemap.xml作为路由sitemap

config.add_route('sitemap', '/sitemap.xml')

注册路由sitemap的视图代码,并使用您的自定义jinja2模板sitemap.jinja2呈现响应。文件扩展名“jinja2”将触发jinja2呈现器的使用。在

^{pr2}$

这将修复由于尝试将模板命名为url而导致的错误。但这混淆了下面显示的渲染器惯例。在

  • *.pt触发变色龙渲染器
  • *.jinja2触发jinja2呈现器
  • *.mako触发mako呈现器
  • *.xml触发xml呈现器(引发第一个错误)

现在仍然需要您根据sitemaps protocol创建XML。但是你的代码看起来很有前途。将资源树传递给XML模板。每个资源通常都可以访问它们的属性,如URL或最近更改的内容。在

相关问题 更多 >