5

我一直在尝试编写一个自定义模板标签来缩短与 bitly 的链接,我附上了我在下面得到的代码和错误。我试图查看 Django 提供的文档,但看不到我做错了什么。

我已将我的模板标签放在以下布局中:

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

我编写的自定义标签文件:

缩短链接.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()

使用模板标签的模板部分:

模板

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

错误

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
4

2 回答 2

8

错误在这一行:

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

模板标签使用{% ... %}(模板过滤器使用{{ ... }})。尝试:

<p>{% bitlyshort "http://www.google.com" %}</p>
于 2012-08-27T00:11:26.480 回答
0

有另一种方法可以导致此错误:

代替:

{{ data|filter }}

如果您使用:

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

字符两侧的空格|会导致页面渲染时出现异常。

于 2014-07-30T04:16:23.647 回答