我没有去view.py,而是希望它转到模板robots.txt。
答案 0 :(得分:237)
使用基于类的通用视图,但使用django 2.0+模式注册。
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('foo/', TemplateView.as_view(template_name='foo.html'))
]
https://docs.djangoproject.com/en/2.0/ref/class-based-views/base/#templateview
使用基于类的通用视图。
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^foo/$', TemplateView.as_view(template_name='foo.html')),
)
urlpatterns = patterns('django.views.generic.simple',
(r'^foo/$', 'direct_to_template', {'template': 'foo_index.html'}),
(r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
)
答案 1 :(得分:12)
更新版本的进一步更新,包括此网站的mime类型:
http://www.techstricks.com/adding-robots-txt-to-your-django-project/
from django.conf.urls import url
from django.views.generic import TemplateView
urlpatterns = [
#... your project urls
url(r'^robots.txt$', TemplateView.as_view(template_name="robots.txt", content_type="text/plain"), name="robots_file")
]