相关疑难解决方法(0)

无法解析剩余的Django

我一直在尝试编写一个自定义模板标签来简单地缩短链接,我附上了代码和错误,我一直在下面.我试着查看Django提供的文档,但看不出我做错了什么.

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

scribbler/
    models.py
    templatetags/
        __init__.py
        shortenlink.py
    views.py
Run Code Online (Sandbox Code Playgroud)

我写的自定义标记文件:

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()
Run Code Online (Sandbox Code Playgroud)

使用模板标记的模板的一部分:

模板

{% load shortenlink %}
<p>{{ bitlyshort "http://www.google.com" }}</p>
Run Code Online (Sandbox Code Playgroud)

错误

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 …
Run Code Online (Sandbox Code Playgroud)

django django-templates

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

django ×1

django-templates ×1