【发布时间】:2011-07-18 04:05:28
【问题描述】:
我使用request.path 来获取当前的URL。例如,如果当前 URL 是“/test/foo/baz”,我想知道它是否以字符串序列开头,比如说 /test。如果我尝试使用:
{% if request.path.startswith('/test') %}
Test
{% endif %}
我收到一条错误消息,提示它无法解析表达式的其余部分:
Could not parse the remainder: '('/test')' from 'request.path.startswith('/test')'
Request Method: GET
Request URL: http://localhost:8021/test/foo/baz/
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '('/test')' from 'request.path.startswith('/test')'
Exception Location: C:\Python25\lib\site-packages\django\template\__init__.py in __init__, line 528
Python Executable: C:\Python25\python.exe
Python Version: 2.5.4
Template error
一种解决方案是创建一个自定义标签来完成这项工作。还有其他东西可以解决我的问题吗?使用的 Django 版本是 1.0.4。
【问题讨论】:
标签: python django templates url django-templates