How to embed a tag within a url templatetag in a django template?
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 }} %}
Asked by: Kevin102 | Posted: 28-01-2022
Answer 1
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.
Answer 2
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".
Answered by: Lucas472 | Posted: 01-03-2022Answer 3
Posted a bug to Django. They should be able to fix this on their side.
http://code.djangoproject.com/ticket/10823
Answered by: Adrian410 | Posted: 01-03-2022Similar questions
python - how to pass value into templatetag from template in django
I have very basic question,gone through many blogs and django documentation but its bit confusing .could any help with below scenario.
Scenario :
i have x table in that four columns
columns : **id name date data **
1 ab 2011-02-03 p
2 bc 2011-02-03 A
3 ab 2011-02-04 A
4 bc 201...
python - how to pass value into templatetag from template in django
I have very basic question,gone through many blogs and django documentation but its bit confusing .could any help with below scenario.
Scenario :
i have x table in that four columns
columns : **id name date data **
1 ab 2011-02-03 p
2 bc 2011-02-03 A
3 ab 2011-02-04 A
4 bc 201...
python - How to create a specific if condition templatetag with Django?
My problem is a if condition.
I would like somethings like that but cannot figure out how to do it.
{% if restaurant.is_favorite_of(user) %}
<img src="{{MEDIA_URL}}images/favorite_on.png" alt="This restaurant is one of your favorite (Click to undo)" />
{% else %}
<img src="{{MEDIA_URL}}images/favorite_off.png" alt="This restaurant is not one of your favorite (Click to add to your ...
python - django templatetag
am looking for a django templatetag that will count words and substring a whole paragraph without chopping off words. Is there a built in function? I tried looking into the built-in function list at Django template documentation but couldn't find anything.
Please advice?
python - how to pass value into templatetag from template in django
I have very basic question,gone through many blogs and django documentation but its bit confusing .could any help with below scenario.
Scenario :
i have x table in that four columns
columns : **id name date data **
1 ab 2011-02-03 p
2 bc 2011-02-03 A
3 ab 2011-02-04 A
4 bc 201...
python - How to render the templatetag Context from views.py using Ajax in Django?
In my template:
<div class="widget-content">
{% render_widget settings %}
</div>
render_widget is a templatetag which takes settings a parameter.
Here we have the code of method defined in templatetag:
def render_widget(settings):
# some processing and then save into context
t = get_template(#)
return t.render(...
python - context KeyError in django templateTag
My app's templatetag code is throwing a KeyError for a missing key (page) in the context variable. In my template, I do not refer to context variables with context.variableKeyName, I just refer to variableKeyName (e.g. {% if is_paginated %}). And in my template, I can refer to the key page without any exceptions.
How should I get the context with the keys it needs into my te...
python - restricted attribute for custom templatetag only on production
I am using this templatetag:
@register.filter
def php_striptags(text, allowed=""):
soup = BeautifulSoup(text)
# list all tags
allowed_tags = allowed.split()
for tag in soup.find_all(True):
if tag.name not in allowed_tags:
tag.unwrap()
return soup.encode_contents().decode('utf8')
It works just fine on development machine but I get this error on pro...
python - how to use custom django templatetag with django template if statement?
I've made a django template tag that counts one of my custom user many-to-many field length:
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def unread_messages_count(context):
user = context['request'].user
return len(user.messages_unread.all())
and within the template itself, I want to show it to user only if it's larger than z...
python - With templatetag raises error
I want to define a variable if a condition is met:
{% if order_item.status.ordering >= 60 and is_client %}
{% with readonly=1 %}
{% else %}
{% with readonly=0 %}
{% endif %}
...some code
{% endwith %}
However, I get the following error:
Invalid block tag: 'else', expected 'endwith'
How can I fix this bug in django?
python - django regroup templatetag does not group properly
I have following template
{% regroup product.hotel.facilities.all by facilitytype as facilities %}
{% for facility in facilities %}
<h5>{{ facility.grouper }}</h5>
<p class="tab-content-title bld">
{% for i in facility.list %}
<li>{{ i }}</li>
{% endfor %}
{% endfor %}
And following model structure:
python - Django templatetag return True or False
I'm overwriting the index.html of the admintemplate in Django.
I want to add an extra field over the sidebar which should only be displayed if a contition is true.
Here is the important part of m index.html:
{% block sidebar %}
<div id="content-related">
{% if action_needed %}
<div class="my-module">
<h2>Foobar</h2>
<p>Display this over the sidebar&l...
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python