未提供项目描述
项目描述
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 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | b55cfcd1ce963f09adac4313952fea46273a0aba0ddbf78957c4436b956721e0 |
|
| MD5 | a2956d7c2829ced53f9dc64e2c4c742c |
|
| BLAKE2b-256 | cb9677d3ded9c3158fa37324450a13a48fd5d37d89f5234eb89e6f9945d34d39 |