I want to use dp/L and dp/L-fit variable of dictionary in html table rows in django template.
Example:
[{'PIR': 3133510.2695076177, 'PVR': 13810.315856115429, 'curve': [{'v': 0.1633026324384349, 'dP/L': 85905.56295072743, 'dP/L-fit': 85818.9286758735, 'error': -0.001008482709130518}]}]
When I use {{ dP/L }}, it gives me an error.
As I know you just can't use special characters in template variable.
So if you want to do use a special characters key in template create a custom templatetags:
create template tag file for app like templatetags/dict_tags.py
from django import template
register = template.Library()
#register.filter
def get_value(d, key):
return d.get(key)
and in template
{% load dict_tags %}
{{ dict_variable | get_value:'dP/L' }}
Related
I want to convert something like that id,name to something like that "id","name" in django template.
I looked at django template built in tags but none of them works.
Does anyone have an idea to achieve it?
you can do this by writing a custom Template Tag:
besides your templates folder add a directory named templatetags.
create convert_tags.py in your templatetags directory:
# convert_tags.py
from django import template
register = template.Library()
#register.filter()
def add_quotes(value: str):
""" Add double quotes to a string value """
return f'"{value}"'
In your templates load convert_tags:
<!-- template.html -->
{% load convert_tags %}
{{ your_str|add_quotes }}
You can simply
"{{ id }}", "{{ name }}"
Or you can define a custom template tag to do this dynamically
from django import template
register = template.Library()
#register.simple_tag
def quote(str):
return f'"{str}"'
And in your template
{{ id | quote }}, {{ name | quote }}
Thanks for everyone,
I solved it by creating a custom template tag
Here's the code :
from django import template
register = template.Library()
def add_quote(var):
return '"{0}"'.format(var)
#register.filter()
def add_quotes(value):
""" Add double quotes to a string value """
excluded_fields = value.split(',')
return ",".join(add_quote(i) for i in excluded_fields)
I tried to do this
and it gives me this error
Could not parse the remainder: '[0].title' from 'posts[0].title'
If posts is a dictionary then you can use {{ posts.title }} in your template.
If posts is a queryset then you can write a custom template filter as:
from django.template.defaulttags import register
...
#register.filter
def get_title(queryset):
return queryset[0].title
Then in your template you can use:
{{ posts|get_title }}
You cannot use [] in templates.
You should try replacing this:
posts[0].title
with this:
posts.0.title
For more details see Variables & Lookups
I have this data that comes from a field in the database:
item_list = Links.objects.filter(visible=True)
In an iteration of item_list there is item.name and item.link. In item.link there could potentially be a string value of
'/app/user/{{user.id}}/'.
When rendering this particular item.link in a Django template, it comes out literally in html output as:
/app/user/{{user.id}}/
when literally I am hoping for it to render as:
/app/user/1/
Is there any way to force the template to recognize this as a compiled value for output?
You have to create a custom template tag:
from django import template
register = template.Library()
#register.simple_tag(takes_context=True)
def render(context, tpl_string):
t = template.Template(tpl_string)
return t.render(context)
And the in your template:
{% load my_tags %}
{% render item.link %}
I have a template tag to generate the url as follows;
<li>Archive</li>
I want the '2013'(year) to be generated automatically based off the current year. There is a tag that can do this {% now 'Y' %} however i cannot use it inside the existing template tag as it just produces errors.
Do i need to create a custom tag to do this?
May be better decision is would be set default parameter at the urls.py ?
Example urls.py:
from django.utils.timezone import now
...
urlpatterns = patterns('app.views',
url(r'^.../$', 'blog_archive', {'year': now().strftime('%Y')}),
)
It is not possible to include one tag inside another like this.
You can create an assignment tag, which stores the tag's result in a context variable instead of outputting it.
The example assignment tag in the docs is for a template tag get_current_time, which you could use instead of the {% now %} tag.
In your mytags.py template tag module:
from django import template
register = template.Library()
#register.assignment_tag
def get_current_time(format_string):
return datetime.datetime.now().strftime(format_string)
In your template:
{% load mytags %}
{% get_current_time "%Y" as year %}
<li>Archive</li>
That is what you want.
Maybe this:
<li>Archive</li>
# just pass the datetime object as 'value'
I have a dictionary like this:
dict_name['keyname.with.manydots']
Problem: I can't do a
{{ dict_name.keyname.with.manydots }}
I know it's not possible to do this with Django's templating.. but what is the best work-around you've found? Thanks!
You could write a custom template filter:
from django import template
register = template.Library()
#register.filter
def get_key(value, arg):
return value.get(arg, None)
And in your template
{{ my_dict|get_key:"dotted.key.value" }}
One possible solution, is to create a template tag with the dictionary as the variable and the key as the argument. Remember to emulate Django's default behavior for attribute lookups that fail, this should not throw an error, so return an empty string.
{{ dict_name|get_value:"keyname.with.manydots" }}
#register.filter
def get_value(dict_name, key_name):
return dict_name.get(key_name, '')