在Django中将图像字段添加到XML站点地图
Google识别XML站点地图()的在Django中将图像字段添加到XML站点地图,django,sitemap,django-sitemaps,Django,Sitemap,Django Sitemaps,Google识别XML站点地图()的标记,我想在我的站点地图中包含一个图像属性 因此,获取封面图像并加载到xml文件中需要类似的内容: for article in articles: print article.cover_image 我还需要为标签加载文章。标题 我在谷歌上搜索了Stack Overflow的一个例子,但令人惊讶的是,我没有找到任何例子,所以非常感谢您的帮助 到目前为止,我的文件: ## sitemaps.py ## from django.contrib.
标记,我想在我的站点地图中包含一个图像属性
因此,获取封面图像并加载到xml文件中需要类似的内容:
for article in articles:
print article.cover_image
我还需要为
标签加载文章。标题
我在谷歌上搜索了Stack Overflow的一个例子,但令人惊讶的是,我没有找到任何例子,所以非常感谢您的帮助
到目前为止,我的文件:
## sitemaps.py ##
from django.contrib.sitemaps import Sitemap
from myproject.article.models import Article
class ArticleSitemap(Sitemap):
priority = 1.0
def items(self):
return Article.objects.order_by('-id').order_by('-pub_date')
def lastmod(self, obj):
return obj.pub_date
## urls.py ##
from myproject.sitemaps import ArticleSitemap
sitemaps = {
"article": ArticleSitemap
}
urlpatterns += patterns ('',
(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
您可以添加一些图像并显示它们。我认为,最好为它做单独的应用程序(继承自django.contrib.sitemaps
)。可以通过
在继承的类中重新定义get_url方法(添加图像信息)
将默认模板更改为渲染图像信息所需的模板
代码如下:
在类中添加这些方法-这些方法与django的sitemap框架中定义的几乎相同,但不同之处在于它准备需要在模板中呈现的数据的方式
class MySItemapClass(Sitemap):
def item():
.........
def __get(self, name, obj, default=None):
try:
attr = getattr(self, name)
except AttributeError:
return default
if callable(attr):
return attr(obj)
return attr
def get_urls(self, page=1, site=None, protocol=None):
# Determine protocol
if self.protocol is not None:
protocol = self.protocol
if protocol is None:
protocol = 'http'
# Determine domain
if site is None:
if Site._meta.installed:
try:
site = Site.objects.get_current()
except Site.DoesNotExist:
pass
if site is None:
raise ImproperlyConfigured("To use sitemaps, either enable the sites framework or pass a Site/RequestSite object in your view.")
domain = site.domain
urls = []
for item in self.paginator.page(page).object_list:
loc = "%s://%s%s" % (protocol, domain, self.__get('location', item))
priority = self.__get('priority', item, None)
url_info = {
'item': item,
'location': loc,
'lastmod': self.__get('lastmod', item, None),
'changefreq': self.__get('changefreq', item, None),
'priority': str(priority is not None and priority or ''),
'images' : get_image(protocol, domain,item), # changed here
}
urls.append(url_info)
return urls
根据需要定义get_image方法
定义自定义模板。
我的看起来像这样-注意定义名称空间(“urlset”)的变化
{%spaceless%}
{urlset%中的url为%1}
{{url.location}
{%if url.images%}
{%用于url.images%中的图像}
{{image}
{%endfor%}
{%endif%}
{%if url.lastmod%}{{url.lastmod|日期:“Y-m-d”}{%endif%}
{%if url.changefreq%}{{url.changefreq}}{%endif%}
{%if url.priority%}{{url.priority}{%endif%}
{%endfor%}
{%endspaceless%}
重写以使用新模板而不是默认模板
url(r'^sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps,'template_name': 'seo/sitemap.xml'}),
url(r'^sitemap-(?P.+)\.xml$,'django.contrib.sitemaps.views.sitemap',{'sitemaps':sitemaps,'template\u name':'seo/sitemap.xml'}),
url(r'^sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps,'template_name': 'seo/sitemap.xml'}),