添加favicon.ico
from django.views.generic.base import RedirectView
favicon_view = RedirectView.as_view(url=r'static/image/favicon.ico', permanent=True)
# 全站图标
path('favicon.ico', favicon_view),
添加robots.txt
在Django项目中添加robots.txt,是为了让搜索引擎只爬取我们想让它爬取的内容。
一般搜索引擎在爬取网站内容时都会先爬取这个文件,然后根据这个文件的设置进行内容的爬取。
方式1
在templates文件夹下创建robots.txt
User-agent: *
Disallow: /media/
Sitemap: https://www.liboer.top/sitemap.xml
Sitemap: https://liboer.top/sitemap.xml
user-agent:允许什么样的浏览器抓取你的网站
Allow:允许抓哪些网页, 允许全部Allow:/
Disallow:不允许抓哪些网页
Sitemap:sitemap
url添加路由
from django.views.generic import TemplateView
# robots
path('robots.txt', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
方式2
如果你的Django静态文件是Nginx转发的
先将robots.txt文件放入项目的static文件夹中,然后在nginx的配置文件中添加如下配置。
location /robots.txt {
alias /path/to/static/robots.txt;
}