没有项目描述
django-sitemaps的Python项目详细描述
sitemap.xml生成使用lxml并支持alternates。IT 只使用Python3的关键字参数来编写自文档代码。
安装
只需pip install django-sitemaps。这个包由一个 python模块,django_sitemaps,包含单个类;没有 需要额外配置。
用法
视图:
from app.pages.sitemaps import PagesSitemap def sitemap(request): sitemap = Sitemap( # All URLs are passed through build_absolute_uri. build_absolute_uri=request.build_absolute_uri, ) # URLs can be added one-by-one. The only required argument # is the URL. All other arguments are keyword-only arguments. for p in Page.objects.active(): url = p.get_absolute_url() sitemap.add( url, changefreq='weekly', priority=0.5, lastmod=p.modification_date, alternates={ code: urljoin(domain, url) for code, domain in PAGE_DOMAINS[p.language].items() }, ) # Adding conventional Django sitemaps is supported. The # request argument is necessary because Django's sitemaps # depend on django.contrib.sites, resp. RequestSite. sitemap.add_django_sitemap(PagesSitemap, request=request) # You could get the serialized XML... # ... = sitemap.serialize([pretty_print=False]) # ... or use the ``response`` helper to return a # ready-made ``HttpResponse``: return sitemap.response( # pretty_print is False by default pretty_print=settings.DEBUG, )
urlconf:
from django_sitemaps import robots_txt from app.views import sitemap urlpatterns = [ url(r'^sitemap\.xml$', sitemap), url(r'^robots\.txt$', robots_txt(timeout=86400)), ... ]
robots_txt函数返回一个可用于生成 包含站点地图URL的robots.txt文件。仅默认站点地图 包含:
User-agent: * Sitemap: <protocol>://<host>/sitemap.xml
可以通过设置sitemaps:
from django.urls import reverse_lazy urlpatterns = [ url(r'^robots\.txt$', robots_txt( timeout=86400, sitemaps=[ '/sitemap.xml', reverse_lazy('articles-sitemap'), ..., ], )), ]