前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Django:添加robots.txt文件

Django:添加robots.txt文件

作者头像
新码农
修改2020-12-25 10:55:47
6470
修改2020-12-25 10:55:47
举报
文章被收录于专栏:新码农博客新码农博客

导读

三种方法,按需使用

方法1:将 robots.txt 放到 templates 目录,修改 urls.py

代码语言:txt
复制
# urls.py
from django.views.generic import TemplateView

url(
    r"^robots\.txt$",
    TemplateView.as_view(template_name="robots.txt", content_type="text/plain"),
),

方法2:不需添加 robots.txt 文件,修改 urls.py

代码语言:txt
复制
# urls.py
from django.http import HttpResponse

url(
    r"^robots\.txt$",
    lambda r: HttpResponse(
        "User-agent: *\nDisallow: /admin", content_type="text/plain"
    ),
),

方法3:将 robots.txt 放到根目录,修改 nginx 配置

代码语言:txt
复制
location  /robots.txt {
    alias  /根目录/robots.txt;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020年12月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 导读
    • 方法1:将 robots.txt 放到 templates 目录,修改 urls.py
      • 方法2:不需添加 robots.txt 文件,修改 urls.py
        • 方法3:将 robots.txt 放到根目录,修改 nginx 配置
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档