How to embed a tag within a url templatetag in a django template?
Asked Answered
H

3

3

How do I embed a tag within a url templatetag in a django template?

Django 1.0 , Python 2.5.2

In 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)

In home_page.html, the following

NUP.HOMEPAGE = {{ NUP.HOMEPAGE }}

is displayed as

NUP.HOMEPAGE = named-url-pattern-string-for-my-home-page-view

and the following url named pattern works ( as expected ),

url template tag for NUP.HOMEPAGE = {% url named-url-pattern-string-for-my-home-page-view %}

and is displayed as

url template tag for NUP.HOMEPAGE = /myhomepage/

but when {{ NUP.HOMEPAGE }} is embedded within a {% url ... %} as follows

url template tag for NUP.HOMEPAGE = {% url {{ NUP.HOMEPAGE }} %}

this results in a template syntax error

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

I was expecting {% url {{ NUP.HOMEPAGE }} %} to resolve to {% url named-url-pattern-string-for-my-home-page-view %} at runtime and be displayed as /myhomepage/.

Are embedded tags not supported in django?

is it possible to write a custom url template tag with embedded tags support to make this work?

{% url {{ NUP.HOMEPAGE }} %}

Humiliating answered 31/10, 2008 at 20:45 Comment(1)
If you click on the time showed near "edited" (like "30 mins ago") you get nice coloured diffBiagi
B
2

Maybe you could try passing the final URL to the template, instead?

Something like this:

from django.core.urlresolvers import reverse

def home_page_view(request):
    NUP={"HOMEPAGE": reverse('named-url-pattern-string-for-my-home-page-view')}    
    variables = RequestContext(request, {'NUP':NUP})
    return render_to_response('home_page.html', variables)

Then in the template, the NUP.HOMEPAGE should the the url itself.

Biagi answered 31/10, 2008 at 21:0 Comment(0)
A
0

That's seems way too dynamic. You're supposed to do

{% url named-url-pattern-string-for-my-home-page-view %}

And leave it at that. Dynamically filling in the name of the URL tag is -- frankly -- a little odd.

If you want to use any of a large number of different URL tags, you'd have to do something like this

{% if tagoption1 %}<a href="{% url named-url-1 %}">Text</a>{% endif %}

Which seems long-winded because, again, the dynamic thing you're trying to achieve seems a little odd.

If you have something like a "families" or "clusters" of pages, perhaps separate template directories would be a way to manage this better. Each of the clusters of pages can inherit from a base templates and override small things like this navigation feature to keep all of the pages in the cluster looking similar but having one navigation difference for a "local home".

Anett answered 31/10, 2008 at 20:59 Comment(0)
S
0

Posted a bug to Django. They should be able to fix this on their side.

http://code.djangoproject.com/ticket/10823

Septicemia answered 15/4, 2009 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.