views:

89

answers:

1

In my webapp, there are a lot of errors or other messages that just show a template that is very close to the URL. At the moment, I have half a dozen static mappers like this:

(r'^/message/foo/$', 'direct_to_template', {'template': 'message/foo.html'}),
(r'^/message/bar/$', 'direct_to_template', {'template': 'message/bar.html'}),

Is there some built-in way I can hook up /message/*/ to show the template message/*.html?

+2  A: 

This is pretty easy. Do it like this:

(r'^/message/(?<name>\d+)/$', 'your_app.views.direct_to_template')

and:

def direct_to_template(name):
  return render_to_response('message/%s.html' % name)
gruszczy
I need to do this in a few places but hell, that is easier than spending more time looking for a more elegant solution. Thanks!
Oli
but you should to check what 'name' contains before use it ;) in other case you can be very surprise :)
bluszcz
Well, he should :-) But I didn't want to provide security stuff, just a simple solution to his problem :-)
gruszczy
Security is a complete non-issue. The regex limits it to just numbers in this case, and just letters and dashes in my implementation.
Oli