118

Instead of going to views.py, I want it to go to to a template, robots.txt.

1

2 Answers 2

279

Django 2+

Note: is valid still as of Django 4+

Use the class based generic views but register with the django 2.0+ pattern.

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

Django 1.5+

Use the class based generic views.

from django.views.generic import TemplateView urlpatterns = patterns('', (r'^foo/$', TemplateView.as_view(template_name='foo.html')), ) 

#Django <= 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'}), ) 
Sign up to request clarification or add additional context in comments.

1 Comment

For 1.5+, do we need to do this for every static HTML?
14

A further update for more recent versions and including mime type from this site:

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") ] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.