Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 517751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:51:59+00:00 2026-05-13T07:51:59+00:00

I have a template filter that performs a very simple task and works well,

  • 0

I have a template filter that performs a very simple task and works well, but I would like to use a decorator on it. Unfortunately the decorator causes a nasty django error that doesn’t make any sense…

Code that works:

@register.filter(name="has_network")
def has_network(profile, network):
    hasnetworkfunc = getattr(profile, "has_%s" % network)
    return hasnetworkfunc()

With Decorator (doesn’t work):

@register.filter(name="has_network")
@cache_function(30)
def has_network(profile, network):
    hasnetworkfunc = getattr(profile, "has_%s" % network)
    return hasnetworkfunc()

Here is the error:

TemplateSyntaxError at /

Caught an exception while rendering:
pop from empty list

I have tried setting break points inside the decorator and I am reasonably confident that it is not even being called…

But just in case here is the decorator (I know someone will ask for it)

I replaced the decorator (temporarily) with a mock decorator that does nothing, but I still get the same error

def cache_function(cache_timeout):
    def wrapper(fn):
        def decorator(*args, **kwargs):
            return fn(*args, **kwargs)
        return decorator
    return wrapper

edit CONFIRMED: It is caused because the decorator takes *args and **kwargs? I assume pop() is being called to ensure filters all take at least one arg?

changing the decorator to this fixes the problem:

def cache_function(cache_timeout):
    def wrapper(fn):
        def decorator(arg1, arg2):
            return fn(arg1, arg2)
        return decorator
    return wrapper

Unfortunately that ruins the generic nature of the decorator :/ what to do now?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:52:00+00:00Added an answer on May 13, 2026 at 7:52 am

    Final Answer: Add an extra argument to the decorator indicating what is being decorated

    There may be something more elegant, but this works.

    from django.core.cache import cache
    from django.db.models.query import QuerySet
    try:
        from cPickle import dumps
    except:
        from pickle import dumps
    from hashlib import sha1
    
    cache_miss = object()
    
    class CantPickleAQuerySet(Exception): pass
    
    def cache_function(cache_timeout, func_type='generic'):
        def wrapper(fn):
            def decorator(*args, **kwargs):
                try:
                    cache_indentifiers = "%s%s%s%s" % (
                                             fn.__module__,
                                             fn.__name__,
                                             dumps(args),
                                             dumps(kwargs)
                                             )
                except Exception, e:
                    print "Error: %s\nFailed to generate cache key: %s%s" % (e, fn.__module__, fn.__name__)
                    return fn(*args, **kwargs)
    
                cache_key = sha1(cache_indentifiers).hexdigest()
    
                value = cache.get(cache_key, cache_miss)
    
                if value is cache_miss:
                    value = fn(*args, **kwargs)
    
                    if isinstance(value, QuerySet):
                        raise CantPickleAQuerySet("You can't cache a queryset. But you CAN cache a list! just convert your Queryset (the value you were returning) to a list like so `return list(queryset)`")
    
                    try:
                        cache.set(cache_key, value, cache_timeout)
                    except Exception, e:
                        print "Error: %s\nFailed to cache: %s\nvalue: %s" % (e, cache_indentifiers, value)
    
                return value
    
            no_arg2 = object()
            def filter_decorator(arg1, arg2=no_arg2):
                if arg2 is no_arg2:
                    return decorator(arg1)
                else:
                    return decorator(arg1, arg2)
    
            if func_type == 'generic':
                return decorator
    
            elif func_type == 'filter':
                return filter_decorator
    
        return wrapper
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 270k
  • Answers 270k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I seem to recall Ms. Hackborn indicating that there are… May 13, 2026 at 1:26 pm
  • Editorial Team
    Editorial Team added an answer Capture the buffer output to a string with the output… May 13, 2026 at 1:26 pm
  • Editorial Team
    Editorial Team added an answer If you are on ColdFusion 8 or higher you can… May 13, 2026 at 1:26 pm

Related Questions

The problem: We want to bind a HierarchicalDataTemplate’s ItemsSource property to a CollectionViewSource, to
I'm currently converting my site from PHP to Django, but as I'm new to
I have a list with items where each have number of properties (A, B,
I have a very simple webforms app that will allow field techs to order
I've been drooling over Django all day while coding up an internal website in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.