Django实用攻略(三)robots.txt及sitemap.xml配置方法

Django2.0中,robots.txt和sitemap.xml的配置。网站内容少,爬虫是不会积极去爬取的。为了让新页面更快的被搜索引擎获得,需要添加robots.txt规则文件,以及网站地图sitemap.xml。网上有很多django添加sitemap的教程,在实际测试的时候,遇到很多坑。在这里记录一下2.0版本具体的配置方法。

robots.txt的配置

1、在模板中,写一个robots.txt文件。

# 文件位置:blog/templates/robots.txt
User-agent: *
Disallow: /shadustZ/admin/
Sitemap: https://www.imzcm.com/sitemap.xml

2、配置urls.py

from django.conf.urls import url
from django.views.generic import TemplateView

urlpatterns = [
    ...
    # 上面是其他url配置
    url('robots.txt', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
]

sitemap.xml的配置

sitemap也可以用上面这样的静态文件配置。但是更新太麻烦了。django有不错的sitemap模块,可以直接使用。 先看看长什么样子吧

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    <loc>https://www.imzcm.com/index.html</loc>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
    </url>
    <url>
    <loc>
        https://www.imzcm.com/article/title/django_shi_yong_gong_lue_1.html
    </loc>
    <lastmod>2018-02-01</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.6</priority>
    </url>
</urlset>

具体方法如下:

1、配置setting.py,添加模块

INSTALLED_APPS = [
# sitemap需要的模块
    'django.contrib.sites',
    'django.contrib.sitemaps',
# 其他模块
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

]

2、更新数据库

引入django.contrib.sites这个模块,一般来说都会报错。因为这个时候,缺少一些配置。这个模块需要在数据库建立一个表,使用如下命令自动建立。(这里用的python3,请使用自己的pyhton命令)

python3 ./manage.py makemigrations
python3 ./manage.py migrate

更新后,数据库会增加一个表django_site

3、添加域名

接着使用django shell添加自己的域名到数据库。

python3 manage.py shell
from django.contrib.sites.models import Site    
Site.objects.create(pk=1, domain='www.imzcm.com', name='www.imzcm.com')

pk代表插入的索引位置,这里为1。

4、配置setting.py,设置网址

添加

SITE_ID = 1

至此,启动django不会报错了。

5、在这里停一下,先编写sitemap需要的处理文件。

# 文件位置:blog/sitemap.py
from django.contrib.sitemaps import Sitemap
from base.models import Article
from django.contrib.sitemaps import GenericSitemap
from django.urls import reverse
import time
import datetime
# 静态页面例子,例如主页、说明页
class StaticViewSitemap(Sitemap):
    priority = 0.5 
    changefreq = 'daily' # 更新周期

    def items(self): 
        return ['index', ] # 指定页面

    def location(self, item): # 返回当前页面地址
        return reverse(item)

# 动态页面,例如文章、标签
class ArticleSiteMap(Sitemap):
    changefreq = "daily"
    priority = 0.6

    def items(self): # 查询对应页面的对象
        return Article.objects.filter(state='1')

    def lastmod(self, obj): # 返回最后更新的时间,方便搜索引擎更新
        return datetime.datetime.fromtimestamp(obj.publish_date)

    def location(self, obj): # 返回当前页面地址
        return reverse('article_name', kwargs={'title': obj.seo_link})

代码比较简单,不懂的地方百度一下就好。其中用到的reverse可以解析url为绝对地址。

6、最后,配置urls.py

from blog.sitemap import ArticleSiteMap
from blog.sitemap import StaticViewSitemap

sitemaps = {
    'static': StaticViewSitemap,
    'blog': ArticleSiteMap,
}

urlpatterns = [
    url('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
]

都配置完了,访问https://host/sitemap.xml看看。例如https://www.imzcm.com/sitemap.xml


作者

NewImaging
  • Shadust
  • 有你,真好~

评论