我希望它转到一个模板 robots.txt,而不是 views.py。
注意: 在 Django 4 + 时仍然有效
使用基于类的通用视图,但注册 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/4.1/topics/class-based-views/#usage-in-your-urlconf
Use the class based generic views.
from django.views.generic import TemplateView urlpatterns = patterns('', (r'^foo/$', TemplateView.as_view(template_name='foo.html')), )
# 姜戈 < = 1.4 Docs: https://docs.djangoproject.com/en/1.4/ref/generic-views/#django-views-generic-simple-direct-to-template
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'}), )
更多最新版本的更新,包括来自本网站的哑剧类型:
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") ]