django-reversetag icon indicating copy to clipboard operation
django-reversetag copied to clipboard

Add handy way of creating partials outside templates

Open tomwys opened this issue 14 years ago • 3 comments

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

tomwys avatar Jan 23 '11 12:01 tomwys

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?

ulope avatar Jan 23 '11 20:01 ulope

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.

tomwys avatar Jan 23 '11 20:01 tomwys

Right. That would definitely be useful. I will add something to the next release.

ulope avatar Jan 23 '11 20:01 ulope