Maybe it's a little bit stupid question, but I didn't find an answer. Is there any way to use increased/decreased variables in django templates?
e.g.{{ some_variable + 1 }}
There's a built-in add filter:
{{ some_variable|add:"1" }}
One way of doing this is by using a django template filter.
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters
def inc(value):
return value+1
and then:
{{ some_variable|inc }}
Inside for loop use forloop.counter that will automatically increase the counter till the records.
{% for a in object_list %}
{{ forloop.counter }}
{% endfor %}
Related
Before showing a text I have to evaluate it.
Instead of
{% if value %} text_true {% else %} text_false {% endif %}
Are there anything like:
{{ text_true if value else text_false }}
For the example in your question, you can use the yesno filter
{{ value|yesno:'text_true,text_false' }}
Not with this exact syntax, but there are the default and default_if_none template filters. Those may be what you're looking for.
https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#default
As someone who is just starting to use Django in my scarce time, I appreciate in advance your time in helping me learn how to make my code cleaner and correct.
I have two lists comprised of two querysets as follows:
company_list = [Company_stats.objects.filter(period__exact=P, company_name__in=master_names)]
industry_list = [Industry_stats.objects.filter(period__exact=P, industry_name__in=master_names)]
I iterate through both lists in my template to create a small table.
{%for c in company_list%}
{%for z in c %}
{{ z.company_name }}
{{ z.nxt_m_ret_est }}
{{ z.nxt_m_ret_rat }}
{% endfor %}
{% endfor %}
{%for c in industry_list%}
{%for z in c %}
{{ z.industry_name }}
{{ z.nxt_m_ret_est }}
{{ z.nxt_m_ret_rat }}
{% endfor %}
{% endfor %}
This works fine, however, since I am using the same code except for z.industry_name vs. z.company_name I was wondering whether you could help me figure out a better way to do this.
I have tried combining the lists into one list with both querysets in it and that works except for the obvious issue that I don't know how to tell it to retrieve z.company_name or z.industry_name depending on the queryset where the data is coming from, because everything became part of the same list.
Once you've changed the field to name on both models you can put both querysets into the same list and then iterate over that.
master_list = [model.objects.filter(period__exact=P, name__in=master_names) for model in (Company_stats, Industry_stats)]
...
{% for l in master_list %}
{% for i in l %}
{{ i.name }}
{{ i.nxt_m_ret_est }}
{{ i.nxt_m_ret_rat }}
{% endfor %}
{% endfor %}
If you want to have the code be more generic, it would help to make your names more generic. Would it be possible for you to change industry_name and company_name to just name throughout your system?
in my views.py i obtain 5 dicts, which all are something like {date:value}
all 5 dicts have the same length and in my template i want to obtain some urls based on these dicts, with the common field being the date - as you would do in an sql query when joining 5 tables based on a common column
in python you would do something like:
for key, value in loc.items():
print key, loc[key], ctg[key], sctg[key], title[key], id[key]
but in django templates all i could come up with is this:
{% for lock, locv in loc.items %}
{% for ctgk, ctgv in ctg.items %}
{% for subctgk, subctgv in subctg.items %}
{% for titlek, titlev in titlu.items %}
{% for idk, idv in id.items %}
{% ifequal lock ctgk %}
{% ifequal ctgk subctgk %}
{% ifequal subctgk titlek %}
{% ifequal titlek idk %}
<br />{{ lock|date:"d b H:i" }} - {{ locv }} - {{ ctgv }} - {{ subctgv }} - {{ titlev }} - {{idv }}
.... {% endifequals & endfors %}
which of course is ugly and takes a lot of time to be rendered
right now i am looking into building a custom tag, but i was wondering if you guys have any feedback on this topic?
Sounds to me like you need to use something other than aligned dicts. How about a dict of a small class, which contains the things you want:
class MyThing:
def __init__(self, loc, ctg, sctg, title, id):
self.loc = loc
self.ctg = ctg
self.sctg = sctg
self.title = title
self.id = id
Would that make your template code a bit less painful? (Apologies if I've misunderstood the problem - I'm having a hard time following your nested ifs!).
Django templates should try to resolve variables with dot as a dictionary lookup (if varaible is an dictionary). So ctg.key equals to ctg[key]. Did you try this:
{% for key, value in loc.items %}
{{ key|date:"d b H:i" }} - {{ value }} - {{ ctg.key }} - {{ subctg.key }} - {{ title.key }} - {{ id.key }}
I have recently upgraded to Django 1.2.5, and now I am having problems with localization, specifically number formatting. For example, in some templates I print the following samples:
data-id="{{ form.instance.id }}"
Which in cases >= 1000, used to evaluate to:
data-id="1235"
But now it actually results in (my localization is pt-BR, our decimal separator is dot):
data-id="1.235"
Which of course is not found when I afterwards query the database by ID. Using a |safe filter solves the problem, but I'm not willing to find all IDs in all templates and safe them.
Usually, I'll only localize the floating points, not the integers. I don't want to disable L10N, because of all the other formatting that is working fine. Is there a way to make this distinction in Django localization? Any other solution is accepted.
data-id="{{ form.instance.id|safe }}"
Also do the job
with django 1.2:
data-id="{{ form.instance.id|stringformat:'d' }}"
or, with django 1.3:
{% load l10n %}
{% localize off %}
data-id="{{ form.instance.id|stringformat:'d' }}"
{% endlocalize %}
or (also with django 1.3):
data-id="{{ form.instance.id|unlocalize }}"
http://docs.djangoproject.com/en/1.3/topics/i18n/localization/#topic-l10n-templates
http://docs.djangoproject.com/en/1.3/ref/templates/builtins/#stringformat
This doesn't really answer your question but check out this section of the docs. It says to use {{ |unlocalize }} filter or:
{% localize on %}
{{ value }}
{% endlocalize %}
{% localize off %}
{{ value }}
{% endlocalize %}
There's probably a better way but I'm thinking that you could write a method that gives you the id as a string in your model for each model you are trying to display the id in a template.
class MyModel(models.Model):
pass
def str_id(self):
return u'%s' % self.id
in your template:
{{ form.instance.str_id }}
This is my template tag in a forloop
{{ product.feature_set.all.1.value }}
i want to change the number 1 to the forloop.counter. is this posible?
like:
{{
product.feature_set.all.forloop.counter.value
}}
It does not work like that, but is there a way to do this?
This doesn't make sense. You should be looping through the queryset itself.
{% for feature in product.feature_set.all %}
{{ feature }}
{% endfor %}
Since #Daniel's answer doesn't satisfy you, I thought you might want to try writing a custom filter. Here is a rough draft:
#register.filter
def custom_m2m(queryset, forloop_counter):
return queryset[forloop_counter].value
You can use it in your template like this:
{% for ... %}
{{ product.feature_set.all|custom_m2m:forloop.counter }}
{% endfor %}