在Django的urls.py中,我如何直接进入模板?
而不是去views.py,我希望它去到一个模板,robots.txt。
Django 1.5+
使用基于类的通用视图:
from django.views.generic import TemplateView urlpatterns = patterns('', (r'^foo/$', TemplateView.as_view(template_name='foo.html')), )
文档: https : //docs.djangoproject.com/en/stable/topics/class-based-views/#simple-usage-in-your-urlconf
Django <= 1.4
文档: 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'}), )
更新版本的进一步更新,包括从本网站的MIMEtypes:
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") ]