Login

Default Template Loading

Author:
nirvdrum
Posted:
July 27, 2007
Language:
Python
Version:
.96
Score:
-3 (after 7 ratings)

One of the things about Django that has always irked me is that there seems to be no standard facility for just loading a page and displaying it, simply. Yes, there is the flatpages module, but it is primarily designed for loading content from a DB. While you specify a custom template and put in junk values for the DB content, it just feels like extra, unnecessary work.

Thinking about the problem some more, I borrowed from the idea of flatpages and implemented a view that will load an otherwise unmapped template off the filesystem and render it. This is very useful when converting an existing site over. Just copy your files over. No need to map anything in the URL conf or use the admin to add any flatpage entries. Since it'll render the template, too, you can even use tags and what not, so it need not be a static page.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Add the following after your URLConf.  It is written as it is so that it can be easily added after any definition for serving static media:

from www.views import default # Update this as appropriate.

# Load the default handler last.  This will try to locate any templates
# on the file system as a last ditch effort.
urlpatterns += patterns('',
    # Pages to load directly from the file system.
    (r'^(?P<template_name>.*)$', default),
)



Then add the following to your view:

from django.template import Context, loader
from django.http import HttpResponse, HttpResponseNotFound

def default(request, template_name):
    try:
        t = loader.get_template(template_name)
        c = Context()
    
        response = HttpResponse(t.render(c))
    
        return response
    
    except:
        return HttpResponseNotFound('Page Not Found')

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

miracle2k (on July 28, 2007):

Can the generic view direkt to a template without explicitly adding every file to urlpatterns?

#

esaj (on July 28, 2007):

Yes, just put ...(?P<template>.*)... in your urlconf pattern.

#

nirvdrum (on July 30, 2007):

I'll have to give esaj's suggestion a try. I had been asking on IRC and noone suggested this, but it seems painfully obvious in retrospect.

#

nirvdrum (on July 30, 2007):

Indeed. It works quite well. I guess it'd be nice if this "tip" were included right in the docs. I'll come up with a patch for that.

#

derivin (on July 30, 2007):

Django actively discourages serving up static data, wether images, pages, etc. for doing this, apache or httplite or server of choice should be used.

#

nirvdrum (on July 31, 2007):

Correct. It is discouraged for questionable performance benefits. One can easily make the argument that forcing one to use external facilities to load static HTML is heavy-weight and the complex environment breaks down once more than a couple devs are involved. Indeed, when converting an existing site, it's foolish to have to install a local copy of apache and set up a ton of rewrite rules until those pages eventually become "django pages". I presume this is the reason it was finally decided to add some utility for serving up static media files and that django doesn't throw up warning if an HTML template doesn't use any template tags.

Moreover, what I was trying to achieve was not necessarily loading static files. It was to leverage the templating system to provide common headers, footers, navbars, etc. to pages that are otherwise "static". Having to map each such page was effectively a waste of time and using the approach outlined in these comments addresses that.

#

dschein2 (on August 2, 2007):

Migrating and old site's crap to a beautiful Django site can be a hassle, for sure. Adding the lambda line below helps too.

  • (r'^(.*)/$', lambda request, path: direct_to_template(request, "%s/index.html" % (path,))),
  • (r'^(?P<template>.*)$', direct_to_template),

#

Please login first before commenting.