Django template values - python

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 %}

Related

Access a view variable within a template

I have a context variable which is dynamic because of a loop I perform:
context['categories'] = choices.CATEGORIES
for category_type, category in context['categories']:
context[category_type] = Article.objects.filter(category=category_type).count()
But in the template it just prints the category type instead of the number which is on the variable but I also need type for a link:
{% for type, category in categories %}
{{category}}
{{type}}
{% endfor %}
How can I access this dynamic variable in a for loop?
You should use the items method in for loop like:
{% for type, category in categories.items %}
{{ category }}
{% endfor %}

Django Python, Pass a dictionary and print it's content in html template

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.

django - showing OrderedDict in templatetag

I have a OrderedDict and I need to show its key, value but I am unable to get its value, I have this dict = ([('sainbath', 'thesailor'), ('HELLO', 'WORLD')]), I have tried this
{{object.specifications}}
<ul>
<li>{% for key in object.specifications %}
{{key}}
{%endfor%}
I am getting this output
OrderedDict([('sainbath', 'thesailor'), ('HELLO', 'WORLD')])
sainbath HELLO
I am getting its key only not value, when I tried this
{{object.specifications}}
<ul>
<li>{% for key,value in object.specifications %}
{{key}} : {{value}}
{%endfor%}
it give me error
Need 2 values to unpack in for loop; got 8.
please tell me how can I get value?
1) Use following in template:
{% for key,value in object.specifications.items %}
{{key}}:{{value}}
{% endfor %}
OR
2)If you are rendering a template with the data as OrderedDict, you can use following approach for the same.
return render_to_response('your_template.html',{'data': sorted(your_ordered_dict.iteritems())})
and in Template you can use the same as below:
{% for key, value in data %}
{{key}}:{{value}}
{% endfor %}
Hope it will help you!!

Accessing variables in view - Django template

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 %}

display list value in template page using django

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.

Categories