带有Google新闻语法的Django Sitemap

时间:2019-02-14 11:30:04

标签: django sitemap

对于Django,我正在使用django.contrib.sitemaps生成基本的sitemap.xml。

当前,我想为Google新闻构建一个新的站点地图,但是语法必须更改。而且我真的不知道如何转换此模板:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<script id="tinyhippos-injected"/>
<url>
<loc>
http://www.url.com/
</loc>
<lastmod>2019-02-14</lastmod>
<changefreq>weekly</changefreq>
<priority>0.7</priority>
</url>
</urlset>

此Google新闻模板:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:n="http://www.google.com/schemas/sitemap-news/0.9">
  <url>
    <loc>http://example.com/</loc>
    <n:news>
      <n:publication>
        <n:name>News paper, blog or similar name</n:name>
        <n:language>en</n:language>
      </n:publication>
      <n:publication_date>2008-12-30T00:00:00+00:00</n:publication_date>
      <n:keywords>2008,news,xml,sitemap,example,keywords,generated</n:keywords>
      <n:title>News Article Title</n:title>
    </n:news>
  </url>
</urlset> 

sitemaps.py

from django.contrib.sitemaps import Sitemap
from wall.models import Articles
from datetime import timedelta
from django.utils import timezone

class ArticleSitemap(Sitemap): #Basic Sitemap
    changefreq = "daily"
    priority = 0.7

    def items(self):
        return Articles.objects.filter(published=True).order_by('-update')

    def lastmod(self, item): 
        return item.date

class NewsSitemap(Sitemap): #For Google News Sitemap
    changefreq = "daily"
    priority = 0.7

    def items(self):
        return Articles.objects.filter(published=True, date__range=[timezone.now() - timedelta(days=3), timezone.now()]).order_by('-update')

    def lastmod(self, item): 
        return item.date

我的问题是:如何插入<n:news>和tohers childs标签?

谢谢

0 个答案:

没有答案