Вопрос

Is there a way to generate sitemaps on the fly and regularly submit them to Google using Pyramid?

I've seen 2 code snippets (here and here) for doing this in Flask, but they don't seem applicable to Pyramid.

Specifically, when I include config.add_route('sitemap', '/sitemap.xml') in __init__.py and then the following view:

@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. What's going on?

EDIT: This is my sitemap.jinja2 file.

<?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.

Add this line to init.py to register URL pattern http://example.com/sitemap.xml as route sitemap

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

register view code for route sitemap and render response with your custom jinja2 template sitemap.jinja2. The file extension `jinja2' will trigger usage of jinja2 renderer.

@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. But that mixed up renderer conventions shown below.

  • *.pt triggers Chameleon renderer
  • *.jinja2 triggers Jinja2 renderer
  • *.mako triggers Mako renderer
  • *.xml triggers XML renderer (that raised your first error)

Now it is still up to you to create XML based on sitemaps protocol. But your code looks promising. You pass your resource tree to the XML template. Every resource usually has access to their properties like URL or last_changed stuff.

Другие советы

Have a look here

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.

Maybe it helps You to generate sitemap on the 'fly' :)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top