I am totally new to python and django. I want to know how to access the dictionary variables in template. I tried few ways but nothing worked out. if i print the variable in the template i receive from my view function this is what i get
{'BSE:INFY': {'instrument_token': 128053508, 'last_price': 1150.3},
'NSE:INFY': {'instrument_token': 408065, 'last_price': 1150}}
I want to loop through the ltp and print something like this
BSE:INFY - 1150.3
NSE:INFY - 1150
EDIT
For ex : in my template
{{ ltp }}
gives the output
{'BSE:INFY': {'instrument_token': 128053508, 'last_price': 1150.3},
'NSE:INFY': {'instrument_token': 408065, 'last_price': 1150}}
now how do i loop through them and print something like above mentioned ?
You can use a for loop in the django template.
EX:
{% for key, value in ltp.items %}
{{ key }} - {{ value.last_price }}
{% endfor %}
You can access any dictionary item into your django template as {{dict.key}}.
Suppose you have following dictionary in your views:
dict = {'first_name':'first','last_name':'last'}
You need to send your dictionary item as follows:
return render(request, self.template_name, {'data': dict})
To access your dictionary data in template, you can use
{{ data.first_name }}
To iterate over dictionary, you can do following
{% for key, item in ltp.items %}
{{ key }} : {{item.instrument_token}}, {{ item.last_price }}
{% endfor %}
Related
I have this function just for learning and want to print out the xxxxx and yyyyy on the html page.
I have tried everything but the best I can get is aaa bbb
def index(request):
template = loader.get_template('bsapp/index.html')
listan = {'aaa':'xxxxxx','bbb':'yyyyyy'}
context = {
'listan': listan
}
return HttpResponse(template.render(context, request))
This is the html page, how would I write it?:
{% for item in listan %}
{{ item }}<br>
{% endfor %}
You can iterate your dictionary in Django as:
{% for key, value in listan.items %}
{{ value }}<br>
{% endfor %}
You can use {{ key }} if you want to display key as well.
I need to display the value of list in template page using django. I have a variable like
data = [('dic1',),('dic2',)]
I pass the value to the template this way:
return render(request,'success.html',{'details':data})
Then in the template I need to show the value. I have used the for loop in the template like:
{% for a in data %}
{{a}}
{% endfor %}
But it displays as
('dic1',)
('dic2',)
when I need to display only the value like below
dic1
dic2
Canyone help me to find out which mistake I did ?
Thanks for your response. I jus to use like below
{% for a in data %}
{{ a.0 }}
{% endfor %}
Now it's working correctly.
I am retrieving model object to my template and I am getting it like this:
value: [{'name_value9': 48}]
When filter the object in the view I am using '.values' like this:
...
name_value.values('name_value' + str(fn))
...
The reason for that is that I have 'name_value1, name_value2, etc... and I need to get to the correct one.
How can I show in the template only the result (value) without the key (name_value9)?
Thanks.
You can print values in your template like this:
{% obj in obj_list %}
{% for key, value in obj.items %}
{{ value }}
{% endfor %}
{% endfor %}
I have the 'affects' field in my MongoDB collection that I use to store a list of values. Looks like this:
{
"_id" : ObjectId("51dc89712ef6af45b0a5f286"),
"affects" : [
"GS",
"WEB",
"DB",
"CB",
"ALL",
"OTHER"
],
}
And in the template (html page) I do this:
{% for change in changes %}
{{ change._id }}
{{ change.affects }}
{% endfor %}
This works perfectly when your field has only one value, for example _id would output like this in my HTML page:
51dc89712ef6af45b0a5f286
When there's multiple values though, the output comes out like this:
[u'GS', u'WEB', u'DB', u'CB', u'ALL', u'OTHER']
Is there a way in jinja2 to iterate over the list of values and have them printed out without the brackets, quotes and u?
Thank you.
You probably need a nested loop in Jinja, try this:
{% for change in changes %}
{{ change._id }}
{% for affect in change.affects %}
{{ affect }}
{% endfor %}
{% endfor %}
I was having trouble with something similar, my fix...
flask app.py
#app.route('/mongo', methods=['GET', 'POST'])
def mongo():
# connect to database
db = client.blog
# specify the collections name
posts = db.posts
# convert the mongodb object to a list
data = list(posts.find())
return render_template('mongo_index.html', blog_info=data)
Then your jinja template might look something like this...
mongo_index.hmtl
{% for i in blog_info %}
{{ i['url'] }}
{{ i['post'] }}
{% endfor %}
The initial object returned from mongodb looking something like this...
[{u'category': u'python', u'status': u'published', u'title': u'working with python', u'url': u'working-with-python', u'date': u'April 2012', u'post': u'some blog post...', u'_id': ObjectId('553b719efec0c5546ed01dab')}]
It took me a while to figure out, I guess if it looks like a list, it doesn't actually mean it is one. :)
I have a dictionary called number_devices I'm passing to a template, the dictionary keys are the ids of a list of objects I'm also passing to the template (called implementations). I'm iterating over the list of objects and then trying to use the object.id to get a value out of the dict like so:
{% for implementation in implementations %}
{{ number_devices.implementation.id }}
{% endfor %}
Unfortunately number_devices.implementation is evaluated first, then the result.id is evaluated obviously returning and displaying nothing. I can't use parentheses like:
{{ number_devices.(implementation.id) }}
because I get a parse error. How do I get around this annoyance in Django templates?
Thanks for any help!
A workaround could be using the keys from number_devices and check in the for loop if it is equal to the key provided by number_devices.
{% for key in number_devices.keys %}
{% for implementation in implementations %}
{% ifequal key implementation.id %} you got it {% endifequal %}
{% endfor %}
{% endfor %}
Seems a bit ugly, but should work.