如何在 django 模板的url 模板标签中嵌入标签?
Django 1.0,Python 2.5.2
在views.py
def home_page_view(request):
NUP={"HOMEPAGE": "named-url-pattern-string-for-my-home-page-view"}
variables = RequestContext(request, {'NUP':NUP})
return render_to_response('home_page.html', variables)
在 home_page.html 中,如下
NUP.HOMEPAGE = {{ NUP.HOMEPAGE }}
显示为
NUP.HOMEPAGE = named-url-pattern-string-for-my-home-page-view
并且以下名为 pattern 的 url 有效(如预期的那样),
url template tag for NUP.HOMEPAGE = {% url named-url-pattern-string-for-my-home-page-view %}
并显示为
url template tag for NUP.HOMEPAGE = /myhomepage/
但是当{{ NUP.HOMEPAGE }}
嵌入{% url ... %}
如下时
url template tag for NUP.HOMEPAGE = {% url {{ NUP.HOMEPAGE }} %}
这会导致模板语法错误
TemplateSyntaxError at /myhomepage/
Could not parse the remainder: '}}' from '}}'
Request Method: GET
Request URL: http://localhost:8000/myhomepage/
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '}}' from '}}'
Exception Location: C:\Python25\Lib\site-packages\django\template\__init__.py in __init__, line 529
Python Executable: C:\Python25\python.exe
Python Version: 2.5.2
我期望在运行时{% url {{ NUP.HOMEPAGE }} %}
解析{% url named-url-pattern-string-for-my-home-page-view %}
并显示为/myhomepage/
.
django 不支持嵌入式标签吗?
是否可以编写带有嵌入式标签支持的自定义 url 模板标签来完成这项工作?
{% url {{ NUP.HOMEPAGE }} %}