10๐
Yes, the place for this is in the view.
I feel like the above example wonโt work โ you canโt iterate over an integer.
numItems = request.GET.get('numItems')
if numItems:
numItems = range(1, int(numItems)+1)
return direct_to_template(request, "mytemplate.html", {'numItems': numItems})
{% for item in numItems %}
{{ item }}
{% endfor %}
144๐
you can coerce a str to an int using the add filter
{% for item in numItems|add:"0" %}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#add
to coerce int to str just use slugify
{{ some_int|slugify }}
EDIT: that said, I agree with the others that normally you should do this in the view โ use these tricks only when the alternatives are much worse.
- [Django]-Django url tag multiple parameters
- [Django]-Location of Django logs and errors
- [Django]-What is a django.utils.functional.__proxy__ object and what it helps with?
39๐
I like making a custom filter:
# templatetags/tag_library.py
from django import template
register = template.Library()
@register.filter()
def to_int(value):
return int(value)
Usage:
{% load tag_library %}
{{ value|to_int }}
It is for cases where this cannot be easily done in view.
- [Django]-Accessing dictionary by key in Django template
- [Django]-How can I save my secret keys and password securely in my version control system?
- [Django]-Django gunicorn sock file not created by wsgi
11๐
The easiest way to do this is using inbuilt floatformat filter.
For Integer
{{ value|floatformat:"0" }}
For float value with 2 precision
{{ value|floatformat:"2" }}
It will also round to nearest value. for more details, you can check https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#floatformat.
- [Django]-Reducing Django Memory Usage. Low hanging fruit?
- [Django]-Django Admin Show Image from Imagefield
- [Django]-How to set current user to user field in Django Rest Framework?
2๐
You should add some code to your view to unpack the GET params and convert them to the values you want. Even if numItems
were an integer, the syntax youโre showing wouldnโt give you the output you want.
Try this:
ctx = dict(request.GET)
ctx['numItems'] = int(ctx['numItems'])
response = render_to_string('persistConTemplate.html', ctx)
- [Django]-Django โ "no module named django.core.management"
- [Django]-Get the list of checkbox post in django views
- [Django]-Handling race condition in model.save()
1๐
In my case one of the items was a string and you can not compare a string to an integer so I had to coerce the string into an integer see below
{% if questions.correct_answer|add:"0" == answers.id %}
<span>Correct</span>
{% endif %}
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
- [Django]-Django migration with uuid field generates duplicated values
- [Django]-How to create a fixture file
0๐
You can do like that: if "select" tag used.
{% if i.0|stringformat:'s' == request.GET.status %} selected {% endif %}
- [Django]-What is the best location to put templates in django project?
- [Django]-How to convert JSON data into a Python object?
- [Django]-Django โ Rollback save with transaction atomic
-1๐
My solution is kind of a hack and very specific..
In the template I want to compare a percentage with 0.9, and it never reaches 1, but all the values are considered string in the template, and no way to convert string to float.
So I did this:
{% if "0.9" in value %}
...
{% else %}
...
{% endif %}
If I want to detect some value is beyond 0.8, I must do:
{% if ("0.9" in value) or ("0.8" in value) %}
...
{% else %}
...
{% endif %}
This is a hack, but suffice in my case. I hope it could help others.
- [Django]-Are sessions needed for python-social-auth
- [Django]-How to query as GROUP BY in Django?
- [Django]-Django signals vs. overriding save method