django-reversetag
django-reversetag copied to clipboard
Add handy way of creating partials outside templates
It will be very helpful if you can easily create partial url in view and pass it to template as a context.
I use following code to achieve that:
class ReversibleConstant(object):
def __init__(self, var):
self.var = var
def resolve(self, context, ignore_failures=False):
return self.var
def create_partial_url(base, *args, **kwargs):
base = ReversibleConstant(base)
args = [ReversibleConstant(arg) for arg in args]
kwargs = dict((name, ReversibleConstant(value)) for name, value in dict(kwargs).iteritems())
result = Reversible(base, args, kwargs)
result.resolve({})
return result
I thought about adding a way to pre reverse partials at the view level but found that I never had any real need to do that since you can just as easily (partialy) reverse in the template and store the result in the context.
Do you have a specific use-case where you need this behaviour?
I have generic object list template with pagination. Urls for pagination are constructed in different ways, like: "all/(page)", "tag/(tag)/(page)". If I use your solution, I will have to create additional template for all my views only to create partial urls.
Right. That would definitely be useful. I will add something to the next release.