Need to print the statement inside for loop only once - Django - python

SO in my template i need to print a statement only once inside the for loop even it iterates many times. my code is :
{% for interview in scheduledinterviews %}
{% if interview.slot.start.date == today.date %}
<div class="row">
<div class="col-xs-12 profile-dash">
<h4>Today's Interviews</h4>
</div>
</div>
I need to Print Todays Interview only once as it satisfies the if condition inside the loop. What Should i do?

You can use the ifchanged tag.
{% ifchanged interview.slot.start.date %}<h4>Today's Interviews</h4>{% endifchanged %}

Sorry to disappoint you but there is no break tag in django template. For me the easiest way to go is to prepare your context data better in the respective view prior to rendering the data.

Related

Nested for loop with if statement in Jinja fails to work

enter image description hereenter code here`
<div class="container">
{% for bookmark_title in bookmarks_titles %}
<div class="container-head">
<h2>{{ bookmark_title.title }}</h2>
<span>{{bookmark_title.description}}</span>
</div>
{% for bookmark in bookmarks %}
{% if bookmark.tag == bookmark_title.title %}
<div class="container-body">
<img src="{{bookmark.favicon}}" alt="icon" /> {{ bookmark["title"]}}
</div>
{% endif %}
{% endfor %}
{% endfor %}
</div>
When I run the HTML the first for loop works as expected however, the second nested for loop and if statement only runs after the parent for loop ends.
But I want to return the "Bookmark" saved under each Bookmark_title.title.
As you can see in the attached image for nested for loop works only after the parent for loop ends.
Any suggestion, please.
In the process of filtering all the bookmarks before passing them to the HTML page. Instead of appending the filtered data from the database.db I was replacing with the new results so only the bookmarks, I filtered using the last title tag in this case Entertainment only passed into the HTML page which caused the issue.
To fix it I filtered all the bookmarks from the database.db since the 'Bookmarks' sheet only has the bookmarks, so I don't need to filter by tag.)
# Previously
for tag in tags:
bookmarks = db.session.query(Bookmarks).filter_by(tag=tag)
Fix
bookmarks = db.session.query(Bookmarks).all()

Why is my loop terminating before all items are looped through?

So I'm using Django and trying to dynamically add a users gists to the page using githubs embedded gists.
this is the loop
{% for i in snippets %}
<div class="tile is-4">
<div style="height:25vh;"><script src="https://gist.github.com/{{i.owner.login}}/{{i.id}}.js"/></div>
</div>
{% endfor %}
the list im looping through contains two items, however only the first is added.
if I do something along these lines however it correctly displays the two different id's
{% for i in snippets %}
{{i.id}}
{% endfor %}
I read that the github script calls document.write(), I have a gut feeling this is where my issue lies.
Would document.write() break my loop?

django template conditional html

I'm not sure if the title is technically correct (sorry, I'm new to python+django)
I have a template page that displays whether an application is running or stopped depends on its status. For example:
If app is running I want to display:
<div class="lt">
<a class="play" title="App running">
<span class="text_play">Running</span>
</a>
</div>
<div class="rt">
<input type="submit" onclick="stop_app()" value="stop" class="stop">
</div>
If the application is not running then show this instead:
<div class="lt">
<input type="submit" onclick="star_app()" value="start" class="play">
</div>
<div class="rt">
<a class="stop" title="Application is not running">
<span class="test_stop">Not Running</span>
</a>
</div>
This is kind of stripped down simplified html but my point is how can I avoid repeating myself?
The template is passed a dictionary of applications that it iterates over to display all the applications and their status (running/stopped). So currently I'm iterating over the dict twice, one for "stopped" apps and one for the "running" apps.
Hope it's clear
Thanks in advance
EDIT: This is what I have tried so far:
{% if application.service.status|lower == "enabled" %}
<div>...display running HTML...</div>
{% else %}
<div>...display the non-runing HTML..</div>
{% endif %}
I just want to know if I'm doing the right thing (DRY?)
What you proposed is pretty DRY.
{% if application.service.status|lower == "enabled" %}
<div>...display running HTML...</div>
{% else %}
<div>...display the non-runing HTML..</div>
{% endif %
Keep in mind you'll rely on the return render(request... for determining the html Django needs construct.
Your proposed solution will choose one or the other. I.e. if your non-running HTML needs to switch to running HTML you won't have access to it without another render.
To be more clear and concise, django templates will construct the appropriate HTML leaving out the alternative options or "conditions".
If you learn a bit of jQuery for example you can have elements of the page switch the currently displayed html. Expanding this to ajax will allow you to get status updates from the server and vice versa.

Could not parse the remainder in template

This is my DjangoTemplates/(mysite)/index.html. I'm trying to do some math in my index.html. I want to display the sum of the entire Stakes.amount_won for each user_name in the for loop. I think I have the syntax correct for the math, but it does not seem to work in the template.
The for loop should go through each user_name, and display their user_name as a link with the Amount Won: (total of amount_won for that user_name) below.
from django.db.models import Sum
<h1> Players </h1>
{% if latest_player_list %}
<ul>
{% for player in latest_player_list %}
<li>{{ player.user_name }} <br>Total Won: {{Stakes.objects.filter(player__user_name).aggregate(Sum('amount_won'))}}
</li>
{% endfor %}
</ul>
<br>
{% else %}
<p>No players are available.</p>
{% endif %}
<h3>New Player</h3>
Sorry if this is noobish. Thank you in advance for the help!
Although the import statement is not part of the template language, this isn't actually your problem - it will just show as text. The actual problem is your function call: Django doesn't allow calls with parentheses in templates. Only functions with no parameters are allowed. You need to do the lookup in the view and pass it in the context.
Of course, this is all clearly explained in the documentation.
The quick answer here is that you're trying to do too much in the template - for example, you can't use a Python import statement in a template at all, so I imagine that's just rendering as text. What you'd generally want here is to do the query logic in your view class, then pass the data to the template for rendering.

Complex comparisons on yesno filter?

I'm quite new at python/django.
I love django's templates, but there is one recurrent thing I cannot find
It's very common for me to have one conditional attribute on a (probably long) HTML tag. Most of the times that's an extra CSS class.
For example, let's say you have two links to switch languages, and you want to enable only one at a time. I find myself doing something like this:
{% get_language_info for LANGUAGE_CODE as l %}
<a href="/path/to/switch/to/spanish"
class="{% ifequal l.code "es" %} disable {% endifequal %}">
<img src="/spanish/flag/url" alt="Spanish">
</a>
<a href="/path/to/switch/to/english"
class="{% ifequal l.code "en" %} disable {% endifequal %}">
<img src="/english/flag/url" alt="English">
</a>
Other examples may be while rendering a list of items and checking for the "selected" one, etc.
On other languages I did this with a ternary operator, but I know that's considered "evil" on the python/django world :-)
I wonder if there's a way to leverage from the "yesno" filter. I've tried to do something like:
{{l.code=="es"|yesno:"disabled,"}}
But I couldn't get it to work... am I missing something?
Thanks in advance.
The problem is simply with your syntax in the code you posted:
{{l.code=="es"|yesno:"disabled,"}}
It's actually processed like:
{{ l.code=={{"es"|yesno:"disabled,"}} }}
(Note: that code doesn't work, it's just meant to illustrate how Django reads it)
What you want is a custom template tag that works like the if templatetag but returns like the yesno filter. You could search around on something like djangosnippets.com; maybe someone has already done the work for you. Otherwise, I'd suggest looking at the Django source code for those two and try to merge them into your own templatetag.
Or you could use jQuery/javascript to set the class:
http://api.jquery.com/attr/
something like this - not tested!
{% get_language_info for LANGUAGE_CODE as l %}
<script>
{% if l.code='es' %}
$('.english').attr('class','disable');
{% else %}
$('.english').attr('class','disable');
{% endif %}
</script>
<a href="/path/to/switch/to/spanish"
id="spanish" class='enable">
<img src="/spanish/flag/url" alt="Spanish" >
</a>
<a href="/path/to/switch/to/english"
id="english" class='enable">
<img src="/english/flag/url" alt="English">
</a>

Categories

Resources