跳转到主要内容

未提供项目描述

项目描述

sitemap.xml 生成使用 lxml 支持替代项。它使用Python 3的仅关键字参数来自我说明代码。

安装

只需 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 can also specify the site and protocol manually should you wish
    # to do so:
    sitemap.add_django_sitemap(
        PagesSitemap, site=...site..., protocol=request.scheme
    )
    # Note! If you're omitting the request you *have* to specify site and
    # protocol yourself.

    # 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 函数返回一个视图,可以用来生成包含sitemap URL的robots.txt文件。默认的sitemap仅包含

User-agent: *
Sitemap: <protocol>://<host>/sitemap.xml

可以通过设置 sitemaps 覆盖sitemap URL列表

from django.urls import reverse_lazy

urlpatterns = [
    url(r'^robots\.txt$', robots_txt(
        timeout=86400,
        sitemaps=[
            '/sitemap.xml',
            reverse_lazy('articles-sitemap'),
            ...,
        ],
    )),
]

项目详情


下载文件

下载适用于您的平台文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

django_sitemaps-2.0.1.tar.gz (4.8 kB 查看哈希值)

上传时间

由以下提供支持