33.Django站点地图

栏目: Python · 发布时间: 7年前

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

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

结构:

33.Django站点地图

setting.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django.contrib.sitemaps', # 新加的

    'mysite.apps.MysiteConfig',
]

models.py

from django.db import models

class Blog(models.Model):

    title = models.CharField(max_length=20)
    content = models.TextField()

    pub_date = models.DateTimeField(auto_now=True)

    # 必须有 转换url用的
    def get_absolute_url(self):
        return '/post/%s.html' % (self.id)

views.py

from django.shortcuts import HttpResponse

from .models import Blog

def index(request):
    # 模拟往数据库添加博客
    for x in [0,1,2,3]:
        Blog(title='title-%d' % x, content='content-%d' % x).save()
    return HttpResponse('ok')

urls.py

from django.contrib import admin
from django.urls import path

# 站点地图相关
from django.contrib.sitemaps import GenericSitemap
from django.contrib.sitemaps.views import sitemap

# 自己使用
from mysite.models import Blog
from mysite.views import index

# 站点地图的数据源
sitemaps = {
    'blog': GenericSitemap({'queryset': Blog.objects.all(), # 查询Blog的所有数据
                            'date_field': 'pub_date'},  # 依据Blog中的pub_date时间
                            priority=0.6)
}

urlpatterns = [
    path('admin/', admin.site.urls),
    # 站点地图的URL  http://127.0.0.1/sitemap.xml
    path('sitemap.xml', sitemap,
         {'sitemaps': sitemaps},
         name='django.contrib.sitemaps.views.sitemap'),
    # 首页
    path('', index)
]

同步数据库

$ python3 manage.py makemigrations
$ python3 manage.py migrate

首先访问 http://127.0.0.1/ 往数据库添加模拟数据。

查看db.sqlite3 33.Django站点地图

然后访问 http://127.0.0.1/sitemap.xml 33.Django站点地图

这样可以提交到搜索引擎中,比如google: https://search.google.com/search-console 33.Django站点地图

自定义Sitemap类

上面我们使用的是Django自带的sitemap类GenericSitemap。这个我们可以自定义。

urls.py

from django.contrib import admin
from django.urls import path

from django.contrib.sitemaps import Sitemap
from django.contrib.sitemaps.views import sitemap

from mysite.models import Blog
from mysite.views import index

# 自定义一个sitemap类
class BlogSitemap(Sitemap):
    changefreq = 'never'
    priority = 0.5

    def items(self):
        return Blog.objects.all()

    def lastmod(self, obj):
        return obj.pub_date

# 添加到数据源中
sitemaps = {
    'blog': BlogSitemap()
}

urlpatterns = [
    path('admin/', admin.site.urls),
    path('sitemap.xml', sitemap,
         {'sitemaps': sitemaps},
         name='django.contrib.sitemaps.views.sitemap'),
    path('', index)
]
  • changefreq:当前条目修改的频率 对应于HTML页面中的 标签。
    'always'
    'hourly'
    'daily'
    'weekly'
    'monthly'
    'yearly'
    'never'
  • priority:优先级 对应于HTML页面中的 标签。
  • items()只是一个返回对象列表的方法。
  • lastmod:最后修改时间 方法应该返回一个datetime时间对象。
  • location:不包含协议和域名,在此示例中没有编写location方法,但你可以自己增加此方法来指定对象的URL。 默认情况下,location()在每个对象上调用get_absolute_url()并将返回结果作为对象的url。也就是说,使用站点地图的模型,比如Entry,需要在模型内部实现get_absolute_url()方法。
  • protocol:网站的协议
  • limit:最大超连接数
  • i18n:boolean属性,是否使用所有语言生产站点地图,默认False

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

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Learn Python the Hard Way

Learn Python the Hard Way

Zed A. Shaw / Addison-Wesley Professional / 2013-10-11 / USD 39.99

Master Python and become a programmer-even if you never thought you could! This breakthrough book and CD can help practically anyone get started in programming. It's called "The Hard Way," but it's re......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码