I need to display all the videos within a certain playlist. I can display all the avaliable playlists by using this loop:
{% for p in playlists %}
{{ p.playlist.name }}
{% endfor %}
How can I display all the videos in each playlist too?
It's hard to say since you don't show your models, but something like this could work:
<ul>
{% for p in playlists %}
<li>{{ p.playlist.name }}
<ul>
{% for v in p.playlist.videos %}
<li>{{ v.name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
I'm assuming your videos have a ForeignKey(Playlist, related_name="videos") field.
Related
im trying to group my objects by year only year in datetime field.
{% regroup notificacoes by criados|date:"Y" as criados_list %}
<ul>
{% for x in criados_list %}
<li>
{{ x.grouper }}
<ul>
{% for y in x.list %}
<li>{{ y.slug }} : {{ y.arquivo }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
this was supposed to work, but is not working
I have this code that I used from twig to display sections according to the same date.
I'm trying to use the same code in Django but I can't set variables in its template system. What is the sane way to do this? The correct way? How do people tackle such a problem?
{% set date = "prout" %}
{% for article in articles %}
{% if article.date != date %}
{% if date != "prout" %}
</ul>
</section>
{% endif %}
{% set date = article.date %}
<section class="row">
<h2>{{ article.date }}</h2>
<ul>
<li>+ {{ article.titre }}</li>
{% else %}
<li>+ {{ article.titre }}</li>
{% endif %}
{% endfor %}
</ul>
</section>
The closer concept to 'set' variables is with tag. Quoting Built-in template tags and filters django docs:
with
Caches a complex variable under a simpler name. This is useful
when accessing an “expensive” method (e.g., one that hits the
database) multiple times.
For example:
{% with total=business.employees.count %}
{{ total }} employee{{ total|pluralize }}
{% endwith %}
I want to get the list of songs under the list of artists.
My artist class just contains the artists first and last name.
My song class contains a foreign key of an artist along with the song title.
I am able to list the artists but when I try to list the songs of the artist I get and error in my {% endif %} that ends my if statement {% if song %}.
{% extends "base.html" %}
{% block heading %}Music Catalog{% endblock %}
{% block content %}
{% if user.username %}
<p>Welcome {{ user.username }}!</p>
{% if artist %}
<u1>
{% for singer in artist %}
<li>{{ singer.firstname }} {{ singer.lastname }}</li>
{% if song %}
<u1>
{% for songs in song %}
{% if (songs.artist.firstname == singer.firstname
and songs.artist.lastname == singer.lastname) %}
<li>{{ songs.title }}</li>
{% endif %}
{% endfor %}
</u1>
{% endif %}
{% endfor %}
</u1>
{% else %}
<p>No artists were found in the music catalog.</p>
{% endif %}
{% else %}
<p>You need to login to see your music catalog.</p>
{% endif %}
{% endblock %}
enter code here
I don't think so, in the template language if statement you can use round bracket it won't parse. Try by removing it...
{% if songs.artist.firstname == singer.firstname and songs.artist.lastname==singer.lastname%}
It seems that your view should be handling more of this logic. Like Raunak Agarwal mentioned if you are passing your song or songs in to the template then each one is going to be the same.
It's very strange as well to be doing a
{% for songs in song %}
That just doesn't read right.
I would visit the view a little closer. I had written some more below. After looking over your code though taking a look at the view as well as the model would shed some light on things and allow for a much better help/answer to be given.
As you said My song class contains a foreign key of an artist along with the song title. - why don't you just use the regroup feature?
{% regroup song by artist as artist_list %}
<ul>
{% for artist in artist_list %}
<li>{{ artist.grouper }}
<ul>
{% for songs in artist.list %}
<li>{{ songs.title }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
Yes, your if song line is incorrect. It's quite clear from the template that you don't even have a song attribute at that point. Where is it supposed to be coming from? Presumably it's a related set on singer, but you haven't said so in the template.
You probably want something like this:
{% for singer in artist %}
<li>{{ singer.firstname }} {{ singer.lastname }}
{% with songs as singer.song_set.all %}
{% if songs %}
<ul>
{% for song in songs %}
<li>{{ song.title }}</li>
{% endfor %}
</uL>
{% endif %}
{% endwith %}
</li>
{% endfor %}
I've also removed that comparison of artist firstname and lastname, that didn't seem to make sense: you're already iterating through the set of songs from that artist, so no comparison is needed.
Hi i've been looking all over and can't find the answer to this. I have only 3 months experience in using python/django so excuse my dummy quesion!
Im using django mptt to display a simple nested set navigation.
<ul class="root">
{% recursetree nodes %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
this works fine - however i would like to show only children of the selected category (based on slug) and not all of them.
Any ideas ???
i finally did it like this:
{% recursetree nodes %}
<li>
<a href='/{{ node.get_absolute_url}}'>{{ node.name }}</a>
</li>
{% if not node.is_leaf.node %}
{% for c in child %}
{% if c in node.get_children %}
{% if forloop.first %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endrecursetree %}
in views
category = get_object_or_404(Category, slug=slug)
child = category.get_children()
if not child :
child = category.get_siblings()
but it is a hack. has anyone got better idea?
You need to send down some information about what node you're in, and then it's a simple if statement.
Regarding how to send down the node information universally, there are a couple ways to do this in Django, and none of them are perfect. My preferred method is context processors: http://docs.djangoproject.com/en/1.3/ref/templates/api/#writing-your-own-context-processors
{% recursetree nodes %}
<li>
{{ node.name }}
{% if node.name == category.name %}
<ul>
{{ children }}
</ul>
{% endif %}
<li>
{% endrecursetree %}
You can try this:
{% recursetree nodes %}
#check if the node is a child node
{% if node.is_child_node %}
<a href="{{ node.get_absolute_url }}" >{{ node.name }}</a>
{% endif %}
#check if a root node is the current category
{% if node.is_root_node and node.name == category.name %}
{{ children }}
{% endif %}
{% endrecursetree %}
I have the following view in my django application
def ViewSale( request ):
salecur = Sale.objects.filter(user=2).order_by('sale_date')
return render_to_response('myapp/sale.html',{'salecur':salecur})
my template looks like this
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
When i render the page i get the grouper sale_date.grouper printed, but {{ sale.item }} and {{ sale.qty }} in the inner loop shows nothing! Blank.
What am i missing?
Gath
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_date.list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
See the documentation on regroup:
{% regroup %} produces a list of group objects. Each group object has two attributes:
grouper -- the item that was grouped by (e.g., the string "Male" or "Female").
list -- a list of all items in this group (e.g., a list of all people with gender='Male').