python - django如何添加robots.txt文件?

查看:109
本文介绍了python - django如何添加robots.txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

普通的网站直接把代码放在根目录就可以了
但是感觉django好像很麻烦啊~~,放根目录不行
有什么比较好的办法吗?难道这么简单的东西都要特地写个视图?那岂不是坑爹!?

解决方案

方法一

1、可以参考 Django 官网所放位置:github > djangoproject.com

2、修改 nginx 配置

location  /robots.txt {
    alias  /path/to/static/robots.txt;
}

方法二

使用第三方库 django-robots

方法三

修改 urls.py,将 robots.txt 放到 templates 目录

# urls.py
from django.views.generic import TemplateView

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

方法四

修改 urls.py,不需添加 robots.txt 文件

# urls.py
from django.http import HttpResponse

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

这篇关于python - django如何添加robots.txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆