国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Django添加sitemap的方法示例

2020-02-15 22:41:50
字體:
來源:轉載
供稿:網友

sitemap是 Google 最先引入的網站地圖協議,采用 XML 格式,它的作用簡而言之就是優化搜索引擎的索引效率,詳細的解釋可以參考百度百科 。

下面介紹下如何為Django站點添加sitemap功能。

1、啟用sitemap

在django的settings.py的INSTALLED_APPS中添加

'django.contrib.sites','django.contrib.sitemaps',

然后migrate數據庫:

$ ./manage.py makemigrations$ ./manage.py migrate

登陸Django后臺,修改SITE為你Django網站的域名和名稱,然后在settings.py中加入SITE_ID = 1來制定當前的站點。

2、添加sitemap功能

(1)創建sitemap

創建sitemap.py.內容類似下面的代碼:

from django.contrib.sitemaps import Sitemapfrom blog.models import Article, Category, Tagfrom accounts.models import BlogUserfrom django.contrib.sitemaps import GenericSitemapfrom django.core.urlresolvers import reverseclass StaticViewSitemap(Sitemap): priority = 0.5 changefreq = 'daily' def items(self):  return ['blog:index', ] def location(self, item):  return reverse(item)class ArticleSiteMap(Sitemap): changefreq = "monthly" priority = "0.6" def items(self):  return Article.objects.filter(status='p') def lastmod(self, obj):  return obj.last_mod_timeclass CategorySiteMap(Sitemap): changefreq = "Weekly" priority = "0.6" def items(self):  return Category.objects.all() def lastmod(self, obj):  return obj.last_mod_timeclass TagSiteMap(Sitemap): changefreq = "Weekly" priority = "0.3" def items(self):  return Tag.objects.all() def lastmod(self, obj):  return obj.last_mod_timeclass UserSiteMap(Sitemap): changefreq = "Weekly" priority = "0.3" def items(self):  return BlogUser.objects.all() def lastmod(self, obj):  return obj.date_joined

(2)url配置

url.py中加入:

from DjangoBlog.sitemap import StaticViewSitemap, ArticleSiteMap, CategorySiteMap, TagSiteMap, UserSiteMapsitemaps = { 'blog': ArticleSiteMap, 'Category': CategorySiteMap, 'Tag': TagSiteMap, 'User': UserSiteMap, 'static': StaticViewSitemap}url(r'^sitemap/.xml$', sitemap, {'sitemaps': sitemaps},  name='django.contrib.sitemaps.views.sitemap'),

至此,全部完成,運行你的django程序,瀏覽器輸入:http://127.0.0.1:8000/sitemap.xml

就可以看見已經成功生成了,然后就可以提交這個地址給搜索引擎。 我的網站的sitemap的地址是:https://www.fkomm.cn/sitemap.xml

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昌都县| 宜君县| 旺苍县| 芜湖市| 密山市| 海丰县| 科尔| 剑河县| 宁津县| 贺兰县| 阿尔山市| 贺州市| 太康县| 潜山县| 怀远县| 渭南市| 额济纳旗| 山阳县| 上饶县| 高清| 沁阳市| 探索| 揭东县| 黄浦区| 昌都县| 顺平县| 明光市| 抚州市| 商城县| 六枝特区| 龙江县| 洪泽县| 丽水市| 雅江县| 刚察县| 昌乐县| 自贡市| 兴隆县| 栾城县| 金平| 津市市|