Django添加sitemap的方法示例
sitemap是 Google 最先引入的網(wǎng)站地圖協(xié)議,采用 XML 格式,它的作用簡而言之就是優(yōu)化搜索引擎的索引效率,詳細的解釋可以參考百度百科 。
下面介紹下如何為Django站點添加sitemap功能。
1、啟用sitemap
在django的settings.py的INSTALLED_APPS中添加
'django.contrib.sites', 'django.contrib.sitemaps',
然后migrate數(shù)據(jù)庫:
$ ./manage.py makemigrations $ ./manage.py migrate
登陸Django后臺,修改SITE為你Django網(wǎng)站的域名和名稱,然后在settings.py中加入SITE_ID = 1來制定當前的站點。
2、添加sitemap功能
(1)創(chuàng)建sitemap
創(chuàng)建sitemap.py.內(nèi)容類似下面的代碼:
from django.contrib.sitemaps import Sitemap from blog.models import Article, Category, Tag from accounts.models import BlogUser from django.contrib.sitemaps import GenericSitemap from django.core.urlresolvers import reverse class 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_time class CategorySiteMap(Sitemap): changefreq = "Weekly" priority = "0.6" def items(self): return Category.objects.all() def lastmod(self, obj): return obj.last_mod_time class TagSiteMap(Sitemap): changefreq = "Weekly" priority = "0.3" def items(self): return Tag.objects.all() def lastmod(self, obj): return obj.last_mod_time class 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, UserSiteMap sitemaps = { '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
就可以看見已經(jīng)成功生成了,然后就可以提交這個地址給搜索引擎。 我的網(wǎng)站的sitemap的地址是:https://www.fkomm.cn/sitemap.xml
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python中單例模式的幾種實現(xiàn)方式及優(yōu)化詳解
下面小編就為大家分享一篇基于Python中單例模式的幾種實現(xiàn)方式及優(yōu)化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01python函數(shù)裝飾器之帶參數(shù)的函數(shù)和帶參數(shù)的裝飾器用法示例
這篇文章主要介紹了python函數(shù)裝飾器之帶參數(shù)的函數(shù)和帶參數(shù)的裝飾器用法,結(jié)合實例形式分析了Python函數(shù)裝飾器中函數(shù)帶多個參數(shù)以及裝飾器帶有多個參數(shù)的具體原理與實現(xiàn)方法,需要的朋友可以參考下2019-11-11Pytorch 定義MyDatasets實現(xiàn)多通道分別輸入不同數(shù)據(jù)方式
今天小編就為大家分享一篇Pytorch 定義MyDatasets實現(xiàn)多通道分別輸入不同數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01基于python實現(xiàn)自動化辦公學(xué)習(xí)筆記(CSV、word、Excel、PPT)
這篇文章主要介紹了基于python實現(xiàn)自動化辦公學(xué)習(xí)筆記,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Pandas數(shù)據(jù)集的分塊讀取的實現(xiàn)
本文主要介紹了Pandas數(shù)據(jù)集的分塊讀取的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08