Need to convert a string to int in a django templa

2019-01-07 13:20发布


I am trying to pass in url parameters to a django template like this...

response = render_to_string('persistConTemplate.html', request.GET)

This the calling line from my views.py file. persistConTemplate.html is the name of my template and request.GET is the dictionary that contains the url parameters.

In the template I try to use one of the parameters like this...

{% for item in (numItems) %}

  item {{item}}

{% endfor %}

numItems is one of the url parameters that I am sending in my request like this...

http:/someDomain/persistentConTest.html/?numItems=12

When I try the for loop above, I get an output like this....

image 1 image 2

I am expecting and would like to see the word image printed 12 times...

image 1 image 2 image 3 image 4 image 5 image 6 image 7 image 8 image 9 image 10 image 11 image 12

Can anyone please tell me what I am going wrong?

6条回答
啃猪蹄的小仙女
2楼-- · 2019-01-07 13:28

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.

查看更多
够拽才男人
3楼-- · 2019-01-07 13:29

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.

查看更多
Evening l夕情丶
4楼-- · 2019-01-07 13:41

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 %}
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-07 13:42

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.

查看更多
仙女界的扛把子
6楼-- · 2019-01-07 13:42

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.

查看更多
手持菜刀,她持情操
7楼-- · 2019-01-07 13:46

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)
查看更多
登录 后发表回答