的URL的第一部分,我用request.path
获取当前的URL。例如,如果当前URL为“/测试/富/巴兹”我想知道,如果它与一个字符串序列开始,让我们说/测试。如果我尝试使用:获得Django的模板
{% 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。
我得到'if'语句格式错误。 – Seitaridis 2011-03-21 15:02:24
其对我的工作很好 – Ted 2012-01-17 09:00:50
您的第一个解决方案正常工作! :) – 2014-08-10 18:13:16