无法解析余下的Django部分

5
我一直在尝试编写一个自定义模板标签来使用bitly缩短链接,我已经附上了代码和错误信息。我尝试查看Django提供的文档,但无法确定我的错误在哪里。
我将我的模板标签放置在以下布局中:
scribbler/
    models.py
    templatetags/
        __init__.py
        shortenlink.py
    views.py

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

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()

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

模板

{% 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

在我的情况下,忘记在“date”过滤器后面加上冒号“:{{ somedate | date:“Y m d” }}”导致了这个错误。 - User
2个回答

8

错误在这一行:

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

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

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

我也尝试过,但是我一直收到“无效的块标签:'bitlyshort',期望为'empty'或'endfor'”错误。 - sankaet
2
@sankaet:听起来你的模板中有一个格式不正确的 for 标签。 - mipadi
@mipadi 是正确的。针对你的第二个错误,请检查其余的模板文件,可能忘记结束一个 for 循环。另外,请确保你的应用在 INSTALLED_APPS 设置中被列出。 - Rahul
是的Rahul,它在installed_apps中。@mipadi我最终将bitly api添加到了django的urlize函数中,之后它就可以正常工作了 :) 感谢您的帮助。 - sankaet

0

引起此错误的另一种替代方法是:

而不是:

{{ data|filter }}

如果你使用:

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

| 字符两侧的空格将在页面呈现期间导致异常。


2
空格似乎不会引起问题。真正会引起问题的是在过滤器参数前放置一个空格,例如 x | date: "D"。你必须使用 date:"D" - Andrew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接