Django urls straight to html template


Question

Learning django & python.

Just set up a new site after doing the tutorial. Now for arguments sake say I want to add a bunch of About us, FAQ basic html pages with very limited dynamic elements do you go ahead and write a new line in my urls.py file for each page? or is their some neat way to say map all * *.html to the relevant .html file directly?

In general even if it does require a view will I have to write a new line in the url.py file for every page?

1
32
8/4/2010 4:58:21 AM

Accepted Answer

As long as there is some uniquely identifying section in the URL, you will not need to create an entry in urls.py for each direct-template url.

For example, you could say that all urls ending in ".html" are referencing a direct file from the templates.

urlpatterns = patterns('django.views.generic.simple',
    (r'(.+\.html)$', 'direct_to_template'),
    # ...
)

Take a look at http://docs.djangoproject.com/en/1.2/ref/generic-views/#django-views-generic-simple-direct-to-template for details.

26
5/17/2012 6:58:56 PM

Currently the best way to do this is using TemplateView from generic class-based views:

from django.views.generic.base import TemplateView

url(r'^$', TemplateView.as_view(template_name='index.html'), name='home'),

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon