[英]Pyramid: How to autogenerate sitemap?
Is there a way to generate sitemaps on the fly and regularly submit them to Google using Pyramid? 有没有一种方法可以实时生成站点地图并使用Pyramid定期将其提交给Google?
I've seen 2 code snippets ( here and here ) for doing this in Flask, but they don't seem applicable to Pyramid. 我已经在Flask中看到了2个代码片段( 此处和此处 ),但是它们似乎不适用于Pyramid。
Specifically, when I include config.add_route('sitemap', '/sitemap.xml')
in __init__.py
and then the following view: 具体来说,当我在
__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)
I get an error: 我收到一个错误:
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5- py2.7.egg/pyramid/registry.py", line 148, in _get_intrs_by_pairs
raise KeyError((category_name, discriminator))
KeyError: ('renderer factories', '.xml')
Changing the view to: 将视图更改为:
@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)
Gets past the KeyError from before, but gives me a 404 when I try navigating to mysite.com/static/sitemap.xml.
从以前克服了KeyError,但是当我尝试导航到
mysite.com/static/sitemap.xml.
时却给了我404 mysite.com/static/sitemap.xml.
What's going on? 这是怎么回事?
EDIT: This is my sitemap.jinja2 file. 编辑:这是我的sitemap.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>
Given you want to establish http://example.com/sitemap.xml as your sitemap URL do that. 假设您要建立http://example.com/sitemap.xml作为站点地图URL,则可以这样做。
Add this line to init .py to register URL pattern http://example.com/sitemap.xml as route sitemap
将此行添加到init .py中,以将URL模式http://example.com/sitemap.xml注册为路由
sitemap
config.add_route('sitemap', '/sitemap.xml')
register view code for route sitemap
and render response with your custom jinja2 template sitemap.jinja2
. 使用自定义的jinja2模板
sitemap.jinja2
注册路线sitemap
查看代码并呈现响应。 The file extension `jinja2' will trigger usage of jinja2 renderer. 文件扩展名“ jinja2”将触发jinja2渲染器的使用。
@view_config(route_name='sitemap', renderer='static/sitemap.jinja2')
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)
This will fix your errors resulting from trying to name your templates like URLs. 这将解决您因尝试将模板(如URL)命名而导致的错误。 But that mixed up renderer conventions shown below.
但这混淆了以下所示的渲染器约定。
Now it is still up to you to create XML based on sitemaps protocol . 现在,仍然可以根据站点地图协议来创建XML。 But your code looks promising.
但是您的代码看起来很有希望。 You pass your resource tree to the XML template.
您将资源树传递给XML模板。 Every resource usually has access to their properties like URL or last_changed stuff.
每个资源通常都可以访问其属性,例如URL或last_changed的东西。
from pyramid.threadlocal import get_current_registry
def mview(request):
reg = get_current_registy
when I look into source there is method get_routes_mapper
in pyramid.config.Configurator
. 当我看着源有方法
get_routes_mapper
在pyramid.config.Configurator
。
Maybe it helps You to generate sitemap on the 'fly' :) 也许它可以帮助您在“飞行”中生成站点地图:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.