sankaet
sankaet

Reputation: 345

Could not parse the remainder Django

I've been trying to write a custom template tag to shorten links with bitly, I've attached the code and error I've been getting below. I've tried to look into the documentation provided by Django but cannot see what it is that I am doing wrong.

I've put my templatetag in the following layout:

scribbler/
    models.py
    templatetags/
        __init__.py
        shortenlink.py
    views.py

the custom tag file that I've written:

shortenlink.py

from django import template
from django.conf import settings
from urllib import urlencode
from urllib2 import urlopen

register = template.Library()

@register.simple_tag
def bitlyshort(the_url):
    endpoint = 'https://api-ssl.bitly.com/v3/shorten?access_token={0}&longUrl={1}&format=txt'
    req = urlencode(endpoint.format(settings.ACCESS_KEY, the_url))
    return urlopen(req).read()

part of the template that uses the template tag:

template

{% load shortenlink %}
<p>{{ bitlyshort "http://www.google.com" }}</p>

error

TemplateSyntaxError at /user/sankaetp/
Could not parse the remainder: ' "http://www.google.com"' from 'bitlyshort "http://www.google.com"'
Request Method: GET
Request URL:    http://localhost:8000/user/sankaetp/
Django Version: 1.4.1
Exception Type: TemplateSyntaxError
Exception Value:    
Could not parse the remainder: ' "http://www.google.com"' from 'bitlyshort "http://www.google.com"'
Exception Location: /Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/django/template/base.py in __init__, line 563
Python Executable:  /Users/sankaetp/virtualenvs/myproject/bin/python
Python Version: 2.7.3
Python Path:    
['/Users/sankaetp/virtualenvs/myproject/bin/django_worksquid',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/djangoembed-0.1.1-py2.7.egg',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/httplib2-0.7.4-py2.7.egg',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/BeautifulSoup-3.2.1-py2.7.egg',
 '/Users/sankaetp/virtualenvs/myproject/lib/python27.zip',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/plat-darwin',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/plat-mac',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/lib-tk',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/lib-old',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/lib-dynload',
 '/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7',
 '/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/plat-darwin',
 '/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/lib-tk',
 '/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/plat-mac',
 '/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages',
 '/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/PIL']
Server time:    Sun, 26 Aug 2012 18:54:26 -0500

Upvotes: 5

Views: 19623

Answers (2)

user3890040
user3890040

Reputation: 11

There is an alternative way to cause this error:

Instead of:

{{ data|filter }}

If you use:

{{ data | filter }}  <!-- Note Spaces -->

The spaces on either side of the | character will cause an exception during page render.

Upvotes: 0

mipadi
mipadi

Reputation: 410662

The error is on this line:

<p>{{ bitlyshort "http://www.google.com" }}</p>

Template tags use {% ... %} (template filters use {{ ... }}). Try:

<p>{% bitlyshort "http://www.google.com" %}</p>

Upvotes: 8

Related Questions