Django: How to write an if statement - django

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.

Related

tags are not getting displayed in template, Django

context['d_tags'] = Blog.objects.filter(is_published=True).values('tags__name').order_by('tags__name').distinct()
print(context['d_tags'])
this prints the out put as below
<QuerySet [{'tags__name': 'ev'}, {'tags__name': 'yoga'}]>
how can I show it on templates, tried the following way
{% for tag in d_tags.all %}
<li>{{ tag }}</li>
{% endfor %}
gives an out put in template as
{'tags__name': 'ev'}
{'tags__name': 'yoga'}
but if I do this way
{% for tag in d_tags.all %}
<li>{{ tag.name }}</li>
{% endfor %}
doesn't gives any thing in display, how I can get just the values in template
You said #root's solution doesnt work, but it should... Are you sure you tried {{tag.tags__name}} and not something else?
Another solution would be opening double for loops...
{% for tag in d_tags %}
{% for tag_name in tag %}
<li>{{ tag_name }}</li>
{% endfor %}
{% endfor %}
But hey, thats really the same thing as saying {{tag.tags__name}}

Django template within a for loop

I have a for loop that looks like this:
<ul>
{% for post in latest_post_list %}
<li>{{ post }}</li>
</ul>
{% empty %}
<p>No posts are available.</p>
{% endfor %}
But I want to wrap each post object in a template, so for example:
{% for post in latest_post_list %}
{% include 'jobposttemplate.html' %}
{% empty %}
{% include 'noposts.html' %}
{% endfor %}
jobposttemplate.html will then include all the various information held within the JobPost model.
I tried:
{% for jobposttemplate.html in latest_post_list %}
{{ jobposttemplate.html }}
{% empty %}
{% include 'noposts.html' %}
{% endfor %}
But, predictably, it didn't work. Basically I'm just trying to avoid having to having to write the html out each time, e.g.:
{% for post in latest_post_list %}
<div style="blah">{{ post.deadline }}
</div>
<div style="schmah">{{ post.created_at }}
</div>
{% empty %}
<p>No posts are available.</p>
{% endfor %}
If you are using app specific subfolders inside your templates/ directory, you need to add them to the file name here too.
{% include 'myapp/jobposttemplate.html' %}
And if you want the template to have access to variables, you can add them to the template's scope using with. The sub-template also has access to all scope variables too.
{% include 'myapp/jobposttemplate.html' with object=post %}

Removing the logged in user's name from the list of users in django

I have a template that displays the numbers of likes and name of the users liked the forum. But I don't want the request.user's (logged in user's) name in the list, I just want the other user's name and not the user itself. How do I achieve this? And also I want the list of the names in the reverse order in the template (now its showing the latest liked user in the last of the list.). Please guide me. Thank you.
forums.html:
{% extends "base.html" %}
{% load forum_tags %}
{% block content %}
<h2>Logged in as -- {{request.user}}</h2>
<h1>Forums:</h1>
{% if forums.count > 0 %}
{% for forum in forums %}
<h2>{{forum.question}}</h2>
<p>{{forum.body | truncatewords:"30"}}</p>
{% if user in forum.likes.all and forum.likes.count > 1 %}
<p>Unlike You and {{forum.likes.count | substract:1}} others liked</p>
{% elif user in forum.likes.all %}
<p>You liked it</p>
{% else %}
<p>Like</p>
{% endif %}
{% for likes in forum.likes.all %}
<li>{{likes.get_full_name}}</li>
{% endfor %}
{% endfor %}
{% else %}
<p>Sorry! No forum to display.</p>
{% endif %}
{% endblock %}
snippet of views.py:
def forums(request):
forums = Forum.objects.all()
c = {'forums': forums}
return render(request, 'forums.html', c)
Use RequestContext() while passing context to template. It will add user context parameter , then you can compare it in your template as
{% for likes in forum.likes.all %}
{% if user != likes.user %}
<li>{{likes.get_full_name}}</li>
{%endif%}
{% endfor %}
Can you try something like that ?
{% for likes in forum.likes.all %}
{% if user != likes.user %}
<li>{{likes.get_full_name}}</li>
{% endif %}
{% endfor %}
I had to compare it with fullname, as I was displaying the users full name in the list. So, this was the solution:
{% for likes in forum.likes.all.reverse %}
{% if user.get_full_name != likes.get_full_name %}
<li>{{likes.get_full_name}}</li>
{% endif %}
{% endfor %}
Hope this will be helpful to someone as it was for me. And thanks for all those who helped me!

Django template: check for empty query set

Is there a way to check for an empty query set in the Django template? In the example below, I only want the NOTES header to be displayed if there are notes.
If I put an {% empty %} inside the "for" then it does display whatever is inside the empty tag, so it knows it's empty.
I'm hoping for something that does not involve running the query twice.
{% if notes - want something here that works %}
NOTES:
{% for note in notes %}
{{note.text}}
{% endfor %}
{% endif %}
Clarification: the above example "if notes" does not work - it still displays the header even with an empty query set.
Here's a simplified version of the view
sql = "select * from app_notes, app_trips where"
notes = trip_notes.objects.raw(sql,(user_id,))
return render_to_response(template, {"notes":notes},context_instance=RequestContext(request))
Edit: the view select selects from multiple tables.
Have a look at the {% empty %} tag.
Example from the documentation
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
<li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>
Link: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for-empty
If you are interested in a table, or some kind of heading if there are results, add the forloop.first:
{% for athlete in athlete_list %}
{% if forloop.first %}
Athlete Name:
{% endif %}
{{ athlete.name }}
{% empty %}
Sorry, no athletes in this list.
{% endfor %}
Try {% if notes.all %}. It works for me.
In your view check whether notes is empty or not. If it is then you pass None instead:
{"notes": None}
In your template you use {% if notes %} as normal.
It's unfortunate that you're stuck using a raw query set - they're missing a lot of useful behavior.
You could convert the raw query set into a list in the view:
notes_as_list = list(notes)
return render_to_response(template, {"notes":notes_as_list},context_instance=RequestContext(request))
Then check it as a boolean in the template:
{% if notes %}
Header
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
You could also make it happen without conversions using forloop.first:
{% for note in notes %}
{% if forloop.first %}
Header
{% endif %}
{{ note.text }}
{% endfor %}
What about:
{% if notes != None %}
{% if notes %}
NOTES:
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
{% else %}
NO NOTES AT ALL
{% endif %}
Your original solution
{% if notes %}
Header
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
Works now with Django 1.7 and thanks to QuerySet caching, it does not cost and extra query.
Often the right way to do this is to use the {% with ... %} tag. This caches the query so it runs only once and also gives you more flexibility with your markup than using {% empty %}.
{% with notes as my_notes %}
{% if my_notes %}
<ul>
{% for note in my_notes %}
<li>{{ note }}</li>
{% endfor %}
</ul>
{% else %}
<p>Sorry, no notes available</p>
{% endif %}
{% endwith %}
With this particular example I'm not sure how useful it is but if you're querying Many-to-Many field, for instance, it's likely what you want to do.
Use {% empty %} in django templates
{% if list_data %}
{% for data in list_data %}
{{ data.field_1 }}
{% endfor %}
{% else %}
<p>No data found!</p>
{% endif %}
We can write above code with {% empty %}.
{% for data in list_data %}
{{ data.field_1 }}
{% empty %}
<p>No data found!</p>
{% endfor %}

Django template question?

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.