Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain the first part of an URL from Django template

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.

like image 468
Seitaridis Avatar asked Mar 18 '11 13:03

Seitaridis


People also ask

How can I get full URL in Django?

Use handy request. build_absolute_uri() method on request, pass it the relative url and it'll give you full one. By default, the absolute URL for request. get_full_path() is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL.

What does {{ this }} mean in Django?

{{ foo }} - this is a placeholder in the template, for the variable foo that is passed to the template from a view. {% %} - when text is surrounded by these delimiters, it means that there is some special function or code running, and the result of that will be placed here.

How do I reference a URL in Django?

Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .

What does form {% URL %} do?

{% url 'contact-form' %} is a way to add a link to another one of your pages in the template. url tells the template to look in the URLs.py file. The thing in the quotes to the right, in this case contact-form , tells the template to look for something with name=contact-form .


2 Answers

You can use the slice filter to get the first part of the url

{% if request.path|slice:":5" == '/test' %}
    Test
{% endif %} 

Cannot try this now, and don't know if filters work inside 'if' tag, if doesn't work you can use the 'with' tag

{% with request.path|slice:":5" as path %}
  {% if path == '/test' %}
    Test
  {% endif %} 
{% endwith %} 
like image 167
Ferran Avatar answered Sep 21 '22 12:09

Ferran


Instead of checking for the prefix with startswith, you can get the same thing by checking for membership with the builtin in tag.

{% if '/test' in request.path %}
    Test
{% endif %} 

This will pass cases where the string is not strictly in the beginning, but you can simply avoid those types of URLs.

like image 43
TheOne Avatar answered Sep 22 '22 12:09

TheOne