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

# 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

# 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配置

location  /robots.txt {
alias /根目录/robots.txt;
}