I use request.path
to obtain the current URL. For example if the current URL is "/test/foo/baz" I want to know if it starts with a string sequence, let's say /test. If I try to use:
{% if request.path.startswith('/test') %}
Test
{% endif %}
I get an error saying that it could not parse the remainder of the expression:
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
One solution would be to create a custom tag to do the job. Is there something else existing to solve my problem? The Django version used is 1.0.4.
Instead of checking for the prefix with startswith, you can get the same thing by checking for membership with the builtin
in
tag.This will pass cases where the string is not strictly in the beginning, but you can simply avoid those types of URLs.
You can not pass arguments to normal python functions from within a django template. To solve you problem you will need a custom template tag: http://djangosnippets.org/snippets/806/
You can't, by design, call functions with arguments from Django templates.
One easy approach is to put the state you need in your request context, like this:
Then you will have an
is_test
variable you can use in your template:This approach also has the advantage of abstracting the exact path test ('/test') out of your template, which may be helpful.
You can use the slice filter to get the first part of the url
Cannot try this now, and don't know if filters work inside 'if' tag, if doesn't work you can use the 'with' tag
I use context processor in such case:
*. Create file core/context_processors.py with:
*. Add record:
in settings.py to TEMPLATES 'context_processors' list.
*. Use
in any template.
From the Philosophy section in this page of Django docs:
You really should write a custom tag or pass a variable to inform the template if the path starts with
'/test'