I needed a way to quickly get a direction of html pages templated such that another person could drop new templates in to a subdirectory and without modifying urls.py or views.py get them up and being displayed.
Now, the direct_to_template view provided django.views.generic.simple can sort of do this with a urlpattern like:
`url(r'^(?P<template>.*\.html)$', direct_to_template)`
But that means your templates, no matter what level in the url hierarchy they are reached at, have to be defined at the root of a template directory. I wanted them retrieved from a specific subdirectory instead so I could provide a little wall for them. Hence this snippet.
To use you would have url pattern that looked like:
`url(r'^foo/(?P<template>.*\.html)$', direct_to_template, {'subdir' : 'subdir/'}),`
Which will template any url that matches <parent url>/foo/bar.html for any 'bar'. The problem is if this is a sub-url pattern match this is going to look for the template "bar.html" when we may actually want it to get the template "<parent url>/foo/bar.html"
- templating
- wildcard
- subdir
The django templating language is quite nice, and specifically limited to guide people in to making their business logic in the view, not in the template itself.
Sometimes it can be difficult to do certain things in the template even though it seems like the most appropriate place to do this.
I have an application that is heavily influenced by the user that is logged in in several ways.
For example let us say I have a list of forums. Each forum has several discussions full of posts. This system of forums and discussions has permissions such that some users can not see some entities. Now, I can produce in the view the query set of what forums a user is allowed to see, and I want this list to display the latest post in each of those forums, but I have to restrict that to the posts that they can see.
Easy enough, I have a method on the Forum object that takes a user object and does the appropriate filter to return the latest post that the user can see. The trick is the template is passed the query set of forums, and then iterates through them using the template language. How can it invoke the method on the forum for the latest post that needs the 'user' variable from the template context? The template language lets me say 'forum.latest_post' but I need to pass in 'user'. 'forum.latest_post(user)' does not work because the template language does not allow it.
This tag lets me specify an object, the method on that object to call, and the variable to pass to that method.
It is not the prettiest thing but with this add on you can do:
`{% method_arg forum latest_post user as post %}`