经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Django » 查看文章
Django Sitemap 站点地图的实现方法
来源:jb51  时间:2019/4/30 8:30:42  对本文有异议

Django 中自带了 sitemap框架,用来生成 xml 文件

Sitemap(站点地图)是通知搜索引擎页面的地址,页面的重要性,帮助站点得到比较好的收录。 白话文就是:一个写了你网站的所有url的xml文件,告诉搜索引擎,请及时收录我的这些地址。

sitemap 很重要,可以用来通知搜索引擎页面的地址,页面的重要性,帮助站点得到比较好的收录。

开启sitemap功能的步骤

settings.py 文件中 django.contrib.sitemaps 和 django.contrib.sites 要在 INSTALL_APPS 中

  1. INSTALLED_APPS = (
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. 'django.contrib.sites',
  9. 'django.contrib.sitemaps',
  10. 'django.contrib.redirects',
  11. #####
  12. #othther apps
  13. #####
  14. )

Django 1.7 及以前版本:

TEMPLATE_LOADERS 中要加入 'django.template.loaders.app_directories.Loader',像这样:

  1. TEMPLATE_LOADERS (
  2. 'django.template.loaders.filesystem.Loader',
  3. 'django.template.loaders.app_directories.Loader',
  4. )

Django 1.8 及以上版本新加入了 TEMPLATES 设置,其中 APP_DIRS 要为 True,比如:

  1. # NOTICE: code for Django 1.8, not work on Django 1.7 and below
  2. TEMPLATES = [
  3. {
  4. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  5. 'DIRS': [
  6. os.path.join(BASE_DIR,'templates').replace('\\', '/'),
  7. ],
  8. 'APP_DIRS': True,
  9. },
  10. ]

然后在 urls.py 中如下配置:

  1. from django.conf.urls import url
  2. from django.contrib.sitemaps import GenericSitemap
  3. from django.contrib.sitemaps.views import sitemap
  4. from blog.models import Entry
  5. sitemaps = {
  6. 'blog': GenericSitemap({'queryset': Entry.objects.all(), 'date_field': 'pub_date'}, priority=0.6),
  7. # 如果还要加其它的可以模仿上面的
  8. }
  9. urlpatterns = [
  10. # some generic view using info_dict
  11. # ...
  12. # the sitemap
  13. url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},
  14. name='django.contrib.sitemaps.views.sitemap'),
  15. ]

但是这样生成的 sitemap,如果网站内容太多就很慢,很耗费资源,可以采用分页的功能:

  1. from django.conf.urls import url
  2. from django.contrib.sitemaps import GenericSitemap
  3. from django.contrib.sitemaps.views import sitemap
  4. from blog.models import Entry
  5. from django.contrib.sitemaps import views as sitemaps_views
  6. from django.views.decorators.cache import cache_page
  7. sitemaps = {
  8. 'blog': GenericSitemap({'queryset': Entry.objects.all(), 'date_field': 'pub_date'}, priority=0.6),
  9. # 如果还要加其它的可以模仿上面的
  10. }
  11. urlpatterns = [
  12. url(r'^sitemap\.xml$',
  13. cache_page(86400)(sitemaps_views.index),
  14. {'sitemaps': sitemaps, 'sitemap_url_name': 'sitemaps'}),
  15. url(r'^sitemap-(?P<section>.+)\.xml$',
  16. cache_page(86400)(sitemaps_views.sitemap),
  17. {'sitemaps': sitemaps}, name='sitemaps'),
  18. ]

这样就可以看到类似如下的 sitemap,如果本地测试访问 http://localhost:8000/sitemap.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  3. <sitemap><loc>http://www.ziqiangxuetang.com/sitemap-tutorials.xml</loc></sitemap>
  4. <sitemap><loc>http://www.ziqiangxuetang.com/sitemap-tutorials.xml?p=2</loc></sitemap>
  5. <sitemap><loc>http://www.ziqiangxuetang.com/sitemap-tutorials.xml?p=3</loc></sitemap>
  6. <sitemap><loc>http://www.ziqiangxuetang.com/sitemap-tutorials.xml?p=4</loc></sitemap>
  7. <sitemap><loc>http://www.ziqiangxuetang.com/sitemap-tutorials.xml?p=5</loc></sitemap>
  8. <sitemap><loc>http://www.ziqiangxuetang.com/sitemap-tutorials.xml?p=6</loc></sitemap>
  9. <sitemap><loc>http://www.ziqiangxuetang.com/sitemap-tutorials.xml?p=7</loc></sitemap>
  10. <sitemap><loc>http://www.ziqiangxuetang.com/sitemap-tutorials.xml?p=8</loc></sitemap>
  11. <sitemap><loc>http://www.ziqiangxuetang.com/sitemap-tutorials.xml?p=9</loc></sitemap>
  12. </sitemapindex>

查看了下分页是实现了,但是全部显示成了 ?p=页面数,而且在百度站长平台上测试,发现这样的sitemap百度报错,于是看了下 Django的源代码:

在这里 https://github.com/django/django/blob/1.7.7/django/contrib/sitemaps/views.py

于是对源代码作了修改,变成了jb51的sitemap的样子,比 ?p=2 这样更优雅

引入 下面这个 比如是 sitemap_views.py

  1. import warnings
  2. from functools import wraps
  3. from django.contrib.sites.models import get_current_site
  4. from django.core import urlresolvers
  5. from django.core.paginator import EmptyPage, PageNotAnInteger
  6. from django.http import Http404
  7. from django.template.response import TemplateResponse
  8. from django.utils import six
  9. def x_robots_tag(func):
  10. @wraps(func)
  11. def inner(request, *args, **kwargs):
  12. response = func(request, *args, **kwargs)
  13. response['X-Robots-Tag'] = 'noindex, noodp, noarchive'
  14. return response
  15. return inner
  16. @x_robots_tag
  17. def index(request, sitemaps,
  18. template_name='sitemap_index.xml', content_type='application/xml',
  19. sitemap_url_name='django.contrib.sitemaps.views.sitemap',
  20. mimetype=None):
  21. if mimetype:
  22. warnings.warn("The mimetype keyword argument is deprecated, use "
  23. "content_type instead", DeprecationWarning, stacklevel=2)
  24. content_type = mimetype
  25. req_protocol = 'https' if request.is_secure() else 'http'
  26. req_site = get_current_site(request)
  27. sites = []
  28. for section, site in sitemaps.items():
  29. if callable(site):
  30. site = site()
  31. protocol = req_protocol if site.protocol is None else site.protocol
  32. for page in range(1, site.paginator.num_pages + 1):
  33. sitemap_url = urlresolvers.reverse(
  34. sitemap_url_name, kwargs={'section': section, 'page': page})
  35. absolute_url = '%s://%s%s' % (protocol, req_site.domain, sitemap_url)
  36. sites.append(absolute_url)
  37. return TemplateResponse(request, template_name, {'sitemaps': sites},
  38. content_type=content_type)
  39. @x_robots_tag
  40. def sitemap(request, sitemaps, section=None, page=1,
  41. template_name='sitemap.xml', content_type='application/xml',
  42. mimetype=None):
  43. if mimetype:
  44. warnings.warn("The mimetype keyword argument is deprecated, use "
  45. "content_type instead", DeprecationWarning, stacklevel=2)
  46. content_type = mimetype
  47. req_protocol = 'https' if request.is_secure() else 'http'
  48. req_site = get_current_site(request)
  49. if section is not None:
  50. if section not in sitemaps:
  51. raise Http404("No sitemap available for section: %r" % section)
  52. maps = [sitemaps[section]]
  53. else:
  54. maps = list(six.itervalues(sitemaps))
  55. urls = []
  56. for site in maps:
  57. try:
  58. if callable(site):
  59. site = site()
  60. urls.extend(site.get_urls(page=page, site=req_site,
  61. protocol=req_protocol))
  62. except EmptyPage:
  63. raise Http404("Page %s empty" % page)
  64. except PageNotAnInteger:
  65. raise Http404("No page '%s'" % page)
  66. return TemplateResponse(request, template_name, {'urlset': urls},
  67. content_type=content_type)

如果还是不懂,可以下载附件查看:zqxt_sitemap.zip

更多参考:

官方文档:https://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号