in my sql table I have decimal data with dot as a separator but the display in my page is done with commas
I would like to display them with dot
in my settings i have this
LANGUAGE_CODE = "fr-fr"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True
my models
class VilleStation(models.Model):
nomVille = models.CharField(max_length=255)
adresse = models.CharField(max_length=255)
cp = models.CharField(max_length=5)
latitude = models.DecimalField(max_digits=9, decimal_places=6)
longitude = models.DecimalField(max_digits=9, decimal_places=6)
in the templates i have this
{% for c in object_list %}
{{c.nomVille}}
{{c.adresse}}
{{c.cp}}
{{c.latitude}}
{{c.longitude}}
{% endfor %}
thank
Give this a whirl, as per the docs, you can turn off number localisation like so:
{{ value|floatformat:"3u" }}
What do the docs say?
Output is always localized (independently of the {% localize off %} tag) unless the argument passed to floatformat has the u suffix, which will force disabling localization.
Be aware of the following note further down the page:
Changed in Django 4.0:
floatformat template filter no longer depends on the USE_L10N setting and always returns localized output.
And subsequently:
The u suffix to force disabling localization was added
Please see the documentation regarding the floatformat template tag
Decimal separator is controlled by two settings in Django settings:
First one is DECIMAL_SEPARATOR (Default: '.')
https://docs.djangoproject.com/en/4.1/ref/settings/#decimal-separator
And second one: USE_L10N (Default: True)
https://docs.djangoproject.com/en/4.1/ref/settings/#use-l10n
in your case use USE_L10N=False
Related
I am currently getting familiar with salt and wonder how I could re-use the values of pillars in other places (sections) in .sls files.
In buildout, I would be able to reference a variable from another section with ${sectionname:varname} to re-use a once defined value. This is especially handy, when dealing with directories (basedir, appdir). buildout example:
['foo']
path = /highway/to/hell
['bar']
path = ${foo:path}/lastexit
When I try to reference another variable in an .sls file, even if it is in the same file, I get always None. salt example:
foo:
path: /highway/to/hell
bar:
path: {{ salt['pillar.get']('foo:path') }}/lastexit
salt-ssh minion1 pillar.get bar:path results in None/lastexit
I have the feeling, that I'm missing something here. Could someone point out, how one does re-use values in salt .sls
You can use jinja to assign a value, e.g.:
{% set base_path = salt['pillar.get']('foo:path','/highway/to/hell') %}
foo:
path: {{ base_path }}
bar:
path: {{ base_path }}/lastexit
In this case "/highway/to/hell" is set as a default in case no value is assigned in the pillar or no pillar is found. For more info, see https://docs.saltstack.com/en/latest/topics/jinja/index.html
i want to generate a list of ip addresses (using range of last 8bits so 120-190 translates to x.x.x.120 - x.x.x.190) in defaults/main.yml in my role, and later use it to create new network interfaces and generate a new config file. I tried this approach:
defaults/main.yml:
ip_list: "{%for address_end in range(50,99)%} 192.168.0.{{address_end}}{%endfor%}"
conf_list: "{%for ip in ip_list%}server {{ip}}:6666 {%endfor%}"
and then to use it in template
template.conf.j2:
{% for conf_line in conf_list %}
{{conf_line}}
{% endfor %}
and all i got in generated config file was:
s
e
r
v
e
r
:
6
6
6
6
s
e
r
v
e
r
1
:
6
so my guess is that i'm not generating a list but just a long string and when I use for loop in template.conf.j2 I iterate over single chars. I tried using answer to this problem but all i got was syntax error. Any ideas for what might help me ?
You should format your vars as JSON lists if you want them to be lists.
1.1.1.1 2.2.2.2 3.3.3.3 is a string.
['1.1.1.1', '2.2.2.2', '3.3.3.3'] will be converted to list.
But there is alternative approach for you:
ip_list: "{{ lookup('sequence', 'start=50 count=12 format=192.168.0.%d', wantlist=True) }}"
conf_list: "{{ ip_list | map('regex_replace','(.*)','server \\1:6666') | list }}"
Kostantin answer was of much help, but i found just realized that generating config entries in my case could be solved in an less complex way. Instead of trying to iterate over list or a string variable variable in jinja template file template.conf.j2 like did with :
{% for conf_line in conf_list %}
{{conf_line}}
{% endfor %}
you could just enter insert new line signs while generating string in defaults/main.yml:
conf_list: "{%for ip in ip_list%}server {{ip}}:6666\n{%endfor%}"
and then just insert that whole string into a template.conf.j2 like this:
{{conf_line}}
nevertheless i have no other idea how to generate list of ip addresses other than the one Konstantin proposed.
I currently have a date that is formatted in unicode:
k = u'2015-02-01'
I tried to add this to a list and change it into a string:
date = []
date.append(str(k))
Then I want to pass this as a Django context to my template.
However, the date is showing up with the following:
'2015-02-01'
How do I just rid of $#39; and replace it with a double quote (")?
Thanks much.
You can try to prevent string escape in template like this:
{{ variable|safe }}
In-view way:
from django.utils.safestring import mark_safe
from django.template import Context
data=mark_safe(data)
inescapable = Context({'data': data}, autoescape=False)
I know this is old but other people might stumble upon this with the same problem
Try
{% autoscape off %} {{ date }} {% endautoscape %}
It worked fine for me
When requesting graphs from google charts the data must be sent as a text array. The csv file has to be pure text with no apostrophes.
however
code fragment
data = repr(textData)
returns data bounded by ' '
this is interpreted as "'" in html
The solution to this is to javascript split method
var par = textData.split(""'") textArray = par[1] // the part without '
rest of code
My variable is as such:
api-20150901r1_6.38
Using Jinja, I need a way to grab the string BEFORE the hyphen. There will always be one hyphen so I dont need to worry about multiple instances.
Assuming input_string is api-20150901r1_6.38
You can use set
{% set mylist = input_string.split('-') %}
This is your value "api" in this case
{{ mylist[0] }}
I'm trying to pick a random element from an array -- is this possible using Liquid/Jekyll?
I can create an array -- and access a given index ... but is there a way to "shuffle" the array and then select an index, and thus get a random element from the array?
prefix: ["Foo", "Bar", "Baz"]
---
{{ page.prefix[1] }}
# outputs "Bar"
The 2018 answer is
{% assign prefix = page.prefix | sample: 2 %}
{{ prefix[0] }}
As the OP asked about Jekyll, this can be found at: https://jekyllrb.com/docs/templates/
Liquid doesn't have a filter for picking a random element from an array or an integer interval.
If you want Jekyll to do that, you would have to create an extension to add that liquid filter.
However, I must point out that doing so would pick a random element every time the page is generated, but not every time the page is viewed.
If you want to get different random values every time you visit a page, your best option is using javascript and letting the client pick a random value. You can use liquid to generate the relevant javascript though.
You may be able to do that just in Liquid, but it could less of generic solution like the one provided by #Brendan. According to this article, you can generate a random liquid number between min & max. So simply:
Assign the min to 0 and max to your array's length.
Loop over the array till you find your random number and pick you element.
Here is an example, get your random array index:
{% assign min = 0 %}
{% assign max = prefix.size %}
{% assign diff = max | minus: min %}
{% assign randomNumber = "now" | date: "%N" | modulo: diff | plus: min %}
Then find your random value:
{{ prefix[randomNumber] }}
You can create a plugin to get a random element. Something like this:
module Jekyll
module RandomFilter
# Use sample to get a random value from an array
#
# input - The Array to sample.
#
# Examples
#
# random([1, 2, 3, 4, 5])
# # => ([2])
#
# Returns a randomly-selected item out of an array.
def random(input)
input.sample(1)
end
end
end
Liquid::Template.register_filter(Jekyll::RandomFilter)
Then do something like this in your template to implement:
{% assign myArray = '1|2|3|4|5 | split: '|' %}
{% assign myNumber = myArray | random %}
Without using a plugin (which might be a requirement if you are using github pages for example) and don't want the choice to be set only at build/rebuild time.
This uses collections as it's data source and some feature flags set in the page front matter.
{% if page.announcements %}
<script>
// homepage callout
var taglines=[
{% for txt in site.announcements %}
{{ txt.content | markdownify | jsonify | append: "," }}
{% endfor %}
]
var selection = document.querySelector('#tagline') !== null;
if(selection) {
document.querySelector('#tagline').innerHTML = taglines[ Math.floor(Math.random()*taglines.length) ];
}
</script>
{% endif %}
I use markdownify to process the content, jsonify to make it JavaScript safe and then append a comma to make my array.
The Javascript then populates one randomly at page load.
Add collection to config.yml
collections:
announcements:
Add flag to page
---
layout: home
title:
slider: true
announcements: true
---
collection content item (test.md)
---
published: true
---
This is a test post
You could adapt Liquid::Drop and whitelist Ruby's sample method.
See https://github.com/Shopify/liquid/blob/master/lib/liquid/drop.rb#L69:
You would need to change:
blacklist -= [:sort, :count, :first, :min, :max, :include?]
to:
blacklist -= [:sort, :count, :first, :min, :max, :include?, :sample]
Next you could just use:
{{ some_liquid_array.sample }}