国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Django Sitemap 站點地圖的實現方法

Django Sitemap 站點地圖的實現方法

2021-06-21 00:59ziqiangxuetang Python

這篇文章主要介紹了Django Sitemap 站點地圖的實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

django 中自帶了 sitemap框架,用來生成 xml 文件

sitemap(站點地圖)是通知搜索引擎頁面的地址,頁面的重要性,幫助站點得到比較好的收錄。 白話文就是:一個寫了你網站的所有url的xml文件,告訴搜索引擎,請及時收錄我的這些地址。

sitemap 很重要,可以用來通知搜索引擎頁面的地址,頁面的重要性,幫助站點得到比較好的收錄。

開啟sitemap功能的步驟

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

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

django 1.7 及以前版本:

template_loaders 中要加入 'django.template.loaders.app_directories.loader',像這樣:

?
1
2
3
4
template_loaders = (
  'django.template.loaders.filesystem.loader',
  'django.template.loaders.app_directories.loader',
 )

django 1.8 及以上版本新加入了 templates 設置,其中 app_dirs 要為 true,比如:

?
1
2
3
4
5
6
7
8
9
10
# notice: code for django 1.8, not work on django 1.7 and below
templates = [
  {
    'backend': 'django.template.backends.django.djangotemplates',
    'dirs': [
      os.path.join(base_dir,'templates').replace('\', '/'),
    ],
    'app_dirs': true,
  },
]

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

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

但是這樣生成的 sitemap,如果網站內容太多就很慢,很耗費資源,可以采用分頁的功能:

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

這樣就可以看到類似如下的 sitemap,如果本地測試訪問 http://localhost:8000/sitemap.xml

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

查看了下分頁是實現了,但是全部顯示成了 ?p=頁面數,而且在百度站長平臺上測試,發現這樣的sitemap百度報錯,于是看了下 django的源代碼:

在這里

于是對源代碼作了修改,變成了本站的sitemap的樣子,比 ?p=2 這樣更優雅

引入 下面這個 比如是 sitemap_views.py

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

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

更多參考:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://code.ziqiangxuetang.com/django/django-sitemap.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色综合久久久久 | 在线成人免费电影 | 欧美一级在线观看 | 日韩福利二区 | 亚洲一区中文字幕在线观看 | 精品免费一区二区 | 亚洲在线视频播放 | 国产传媒视频 | 久久久91| 国内精品视频 | 亚洲一区精品在线 | 日韩成人在线一区二区 | 五月婷婷在线视频 | 久久久精品国产 | 国产精品综合一区二区 | 国产欧美精品一区二区三区四区 | 日韩免费一区 | 亚洲成熟少妇视频在线观看 | 网站黄色在线观看免费 | 岛国一区 | 91精品国产综合久久久久久 | 电影91久久久 | 日韩在线视频免费观看 | 亚洲美女精品视频 | 日本中文在线 | a免费网站 | 成人免费小视频 | 国产精品成人一区二区三区夜夜夜 | 日韩在线视频一区 | 欧美精品在线一区 | 久久99精品久久久久久园产越南 | 人成久久| 国产麻豆乱码精品一区二区三区 | 欧美一区二区三区电影 | 国产激情偷乱视频一区二区三区 | 中文字幕乱码亚洲精品一区 | 91精品国产一区二区三区香蕉 | 亚洲成年人网站在线观看 | 日韩一区二区中文 | 国产精品69毛片高清亚洲 | 91成人免费在线观看 |