Prototyping Interfaces with Django (CBV version)

Dave Bertrand over on the Imaginary Landscape blog had a nice tip on a convenient way to do UI prototyping with Django templates. I like this approach since, as Dave points out, you can take full advantage of the Django template language (template tags, control structures etc) and it means when you're done prototyping you are left with usable Django templates rather than plain HTML files.

Dave's post uses django.views.generic.simple.direct_to_template which has been deprecated in Django 1.5. It's easy enough to modify his solution to use a TemplateView instead of the direct_to_template generic view. Just create a little MockUp class in your urls.py file like the following:

from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView

class MockUp(TemplateView):
    def get_context_data(self,**kwargs):
        self.template_name = self.kwargs["template_name"]
        return super(MockUp,self).get_context_data(**kwargs)

urlpatterns = patterns('',
    url(r'^(?P<template_name>.*)$', MockUp.as_view()),    
)

You can now run the dev server and visit 127.0.0.1/foo.html and have your django template foo.html rendered.

It's not quite as slick as the direct_to_template version but it does give you the opportunity to stick a couple other things in the template context if needed.

comments powered by Disqus