I have a json fron an API like this:
my views.py looks like this:
def index(request):
movieData = requests.get('https://api.themoviedb.org/3/search/movie?query=Ishtar&api_key=....').json()
return render(request, 'dashboard/index.html', {'movieData': movieData})
My html looks like this:
{% for item in movieData %}
<lu>
<li>
{{ item.results.id }}
</li>
</lu>
{% endfor %}
However the loop is not working, it's not getting the data, this is how it looks:
Can someone give me hand please? I'm learning.
I'm trying to get the id of the 4 movies on the json result
Many thanks
You don't seem to be iterating over the right thing. The array is inside results.
{% for item in movieData.results %}
<li>{% item.id %}</li>
{% endfor %}
This is the correct answer:
{% for item in movieData.results %}
<li>{{ item.id }}</li>
{% endfor %}
Related
So I am requesting spot price from cex API, it returns something like this
{"data":[{"amount: "67000.0", "base": "BTC", "currency": "USD"}]} of course there is more than one returned so I want to loop over it.
In my views.py I am passing it into my context 'price': price. Then inside of my .html file I have a list with a for loop for example:
<ul>
{% for x in price %}
<li>{{ x }}</li>
{% endfor %}
</ul>
Then when I open my .html page I get data But what I'd like to be able to do is make a table where I have three columns to show the amount, base and currency. I am unsure on how to extract this data individualy?
You can enumerate over the .values of price, and then enumerate over the dictionaries in that list with:
<table>
{% for vs in price.values %}
{% for v in vs %}
<tr><td>{{ v.amount }}</td><td>{{ v.base }}</td><td>{{ v.currency }}</td></tr>
{% endfor %}
{% endfor %}
</table>
In Django templates, you can access to dictionary items with a expresión like {{ dictionary.key }}
<ul>
{% for x in price %}
<li>{{ x.amount }}</li>
{% endfor %}
</ul>
See the docs on https://docs.djangoproject.com/en/3.2/ref/templates/api/#variables-and-lookups
I want to get data from database and send that to html page using django-python.
What I'm doing in python file is
def module1(request):
table_list=student.objects.all()
context={'table_list' : table_list}
return render(request,'index.html',context)
And in html is
<div class="rightbox">
In right box. data is :<br> <br>
{% if table_list %}
<ul> {% for item in table_list %}
<li>{{ item.name }}</li>
<li>{{ item.address }}</li>
<li>{{ item.mob_no }}</li>
{% endfor %}
</ul>
{% else %}
<p>somethings is wrong</p>
{% endif %}
</div>
Nothing is being sent to html file. It is constantly going in else block.
I don't know where I'm making mistake.Please help me.
So far as we don't see your student model (which should be in your models.py and be imported in your vievs.py) and your code doesn't throw any exceptions it seems that your table_list is empty. To iterate it in more convenient way you can use built in for ... empty template tag:
<div class="rightbox">
In right box. data is :<br> <br>
<ul>
{% for item in table_list %}
<li>{{ item.name }}</li>
<li>{{ item.address }}</li>
<li>{{ item.mob_no }}</li>
{% empty %}
<p>somethings is wrong</p>
{% endfor %}
</ul>
</div>
Try this and see what happens. If you'll find yourself in {% empty %} block - your table_list is empty which is points to empty table in database.
Also check docs for this tag.
I have the following code:
<ul>
{% for item in array %}
<li>{{ item }}</li>
{% endfor %}
</ul>
I want to translate the item variable, I've tried using the trans tag like this:
<ul>
{% for item in array %}
<li>{% trans item %}</li>
{% endfor %}
</ul>
But Django complains with a syntax error stating that it was expecting an empty or an endfor
You need to add {% load i18n %} at the top of your template to use the trans tag.
From the docs on internationalization:
To give your template access to these tags, put {% load i18n %}
toward the top of your template.
I have a template variable product_list, which is a QuerySet of Product objects; Product objects, in turn, have a one-to-many field of Track objects (reverse mapped from Track, of course), which is possible to be empty. I want to create a list of Tracks, grouped by Products like this:
{% for product in product_list %}
{% if this is not the first product with tracks %}
<li class="separator"></li>
{% endif %}
{% for track in product.tracks %}
<li>{{track}}</li>
{% endfor %}
{% endfor %}
The question is, what should I write of if this is not the first product with tracks? I tried ifchanged product but it inserts a separator even on the first iteration (as it changes from "" to "someproduct"). forloop.counter is also not usable here, as it is possible that the first two products won’t have tracks.
One workaround could be to change product_list to track_list like this:
track_list = Track.objects.order_by('product__name')
so I can indeed use ifchanged. It is doable in my case, but I’m still interested in a solution for my first method.
You should compose the condition. It looks simple, perhaps is not this that your are asking for.
{% for product in product_list %}
{% if not forloop.first and product.tracks %}
<li class="separator"></li>
{% endif %}
{% for track in product.tracks %}
<li>{{track}}</li>
{% endfor %}
{% endfor %}
If this is not a solution for you, I suggest to you to cook data on view and send ready to be parse to template, more easy.
{% for product in product_list %}
{% if product.should_have_separator %}
<li class="separator"></li>
{% endif %}
{% for track in product.tracks %}
<li>{{track}}</li>
{% endfor %}
{% endfor %}
In your view append should_have_separator field dynamically to products should have it:
product_list = Product.objects.....
is_the_first_product = True
for product in product_list:
is_the_first_product_with_tracks = ( is_the_first_product
and bool( product.tracks ) )
product.should_have_separator = not is_the_first_product_with_tracks
is_the_first_product = False
Let me try to explain what I want to achieve by a small example application. It is a shopping list application that lets you add predefined Items to your ShoppingList as Groceries. The smart thing would be that every Item is related to a Store.
When I am done adding groceries to my shopping list, I should be able to see my shopping list with the groceries organized (grouped) by their Store. Something like:
How would I create a view and template to group and display this shopping list?
For example: this is how I would display the full inventory of all stores:
#views.py
store_list = Store.objects.all()
#template.html
{% for store in store_list %}
<div>{{store}}
<ul>
{% for item in store.item_set.all %}
<li>{{item}}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
That won't work for my shopping list, but I am really looking for something equally powerful / elegant....
What about the built in regroup template tag?
{% regroup item.objects.all by store as store_groups %}
<ul>
{% for store_group in store_groups %}
<li>{{ store_group.grouper }}
<ul>
{% for item in store_group.list %}
<li>{{ item }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
Do the grouping in the backend of the view instead of the template.
In your view, create a dictionary like this:
sorted_groceries = {
store1 : [item1, item2],
store2 : [item3, item4],
}
which can be done with some code resembling this pseudo-code:
sorted_groceries = {}
for item in shopping cart:
try:
sorted_groceries[item.store].append(item)
except KeyError:
sorted_groceries[item.store] = [item]
then add it to your context and have your template look like this:
{% for store, items in sorted_groceries.items %}
<div>{{store}}
<ul>
{% for item in items %}
<li>{{item}}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
How about using ifchanged https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#ifchanged
Assuming shopping_list is sorted by store:
{% for item in shopping_list %}
{% ifchanged %}<h3>{{ item.store.name }}</h3>{% endifchanged %}
{{ item.name }}
{% endfor %}