How do I go straight to template, in Django's urls.py? How do I go straight to template, in Django's urls.py? python python

How do I go straight to template, in Django's urls.py?


Django 2.0+

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

from django.urls import pathfrom django.views.generic import TemplateViewurlpatterns = [    path('foo/', TemplateView.as_view(template_name='foo.html'))]

https://docs.djangoproject.com/en/2.0/ref/class-based-views/base/#templateview

Django 1.5+

Use the class based generic views.

from django.views.generic import TemplateViewurlpatterns = 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'}),)


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 urlfrom django.views.generic import TemplateViewurlpatterns = [    #... your project urls    url(r'^robots.txt$', TemplateView.as_view(template_name="robots.txt", content_type="text/plain"), name="robots_file")]