Search code examples
pythondjangoweb-applicationsbeautifulsoupwebfaction

restricted attribute for custom templatetag only on production


I am using this templatetag:

@register.filter
def php_striptags(text, allowed=""):
    soup = BeautifulSoup(text)

    # list all tags
    allowed_tags = allowed.split()

    for tag in soup.find_all(True):
        if tag.name not in allowed_tags:
            tag.unwrap()

    return soup.encode_contents().decode('utf8')

It works just fine on development machine but I get this error on production:

Exception Type:     RuntimeError
Exception Value:    restricted attribute
Exception Location:     /usr/local/lib/python2.7/inspect.py in getargspec, line 813

I am hosting my site on webfaction, running with apache and mod_wsgi. What could be wrong?


Solution

  • Finally found the real issue, which actually was documented in BeautifulSoup doc: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#other-parser-problems

    If your script works on one computer but not another, it’s probably because the two computers have different parser libraries available. For example, you may have developed the script on a computer that has lxml installed, and then tried to run it on a computer that only has html5lib installed. See Differences between parsers for why this matters, and fix the problem by mentioning a specific parser library in the BeautifulSoup constructor.

    So to make your soup, try something like:

    soup = BeautifulSoup(text, "html.parser")