I am passing in results to my HTML file, which contains a tuple and each of the elements in the tuple is a dictionary. The row element contains five fields, one of which is url. I want to insert a different image for each element in row based on its url field. I also passed in three different url variables to my HTML file, each containing one of the possible urls as a string. Can I use jinja if statements to check equality of two strings (one being the value of the url field in rows and the other being the value of the url variables I pass into the file)?
<div>
{% for row in rows %}
{% if (row.url = trumba_url) %}
<img class="icon" src='https://pbs.twimg.com/profile_images/529755616571432960/uAELf1Xz_400x400.png'></img>
{% endif %}
{% if (row.url = ilab_url) %}
<img class="icon" src='https://yt3.ggpht.com/-9SPiNJ7tggM/AAAAAAAAAAI/AAAAAAAAAAA/6IQqBohqQ-A/s900-c-k-no-mo-rj-c0xffffff/photo.jpg'></img>
{% endif %}
{% if (row.url = hbs_url) %}
<img class="icon" src='http://www.davidkhurst.com/wp-content/uploads/2013/09/Harvard_shield-Business.png'></img>
{% endif %}
<p class="event-title"><a href='{{ row.url }}'>{{ row.title }}</a></p>
<p class="event-date">{{ row.date }}</p>
<p class="event-location">{{ row.location }}</p>
<br></br>
{% endfor %}
</div>
In my python file which runs the website using flask, I have this as well as some more code that defines the tuples in results[].
trumba_url = 'http://www.trumba.com/events-calendar/ma/boston/harvard-events/harvard-event-calendar/harvard-activities/gazette'
ilab_url = 'https://i-lab.harvard.edu/events/'
hbs_url = 'http://www.hbs.edu/mba/admissions/admissions-events/Pages/default.aspx'
return render_template("index.html", results=results, trumba_url=trumba_url, ilab_url=ilab_url, hbs_url=hbs_url)
The equality operator (a comparison operator) in python is ==; = is an assignment operator. Thus, the comparison conditional should be:
{% if (row.url == trumba_url) %}
...otherwise the value of trumba_url would be assigned to row.url.
Try using {% if row.url == trumba_url %}.
Related
I have a list of numbers in views.py list=[0,1,2,3,4,5] (it is not the same all the time I'm fetching it from API)
then in my search.html I have written the following code:-
{% for i in list %}
<a href="https://example.com//{{ id[i] }}">
<img class="row_poster" src="https://this.isapi.org/{{poster[i]}}" alt="" />
<h4>{{title[i]}}</h3>
</a>
{% endfor %}
where the id, poster, title are lists passed through views.py
I want to get elements of each list 1 by 1 I tried adding for loop for each but the result was bad all posters render first and then titles were rendering I want to know how can I use it to call items from the list or any other by which I can access elements for all lists in a single loop.
I have also tried {% with abcd=i %} but the result was the same keep getting this error
Could not parse the remainder: '[i]' from 'id[i]'
To achieve that, first create your own filter:
#register.filter
def get_index(lst, i):
return lst[i]
Which you can then use in your template like:
{% for i in list %}
<a href="https://example.com//{{ id|get_index:i }}">
<img class="row_poster" src="https://this.isapi.org/{{poster|get_index:i}}" alt="" />
<h4>{{title|get_index:i}}</h3>
</a>
{% endfor %}
For information on how to register custom tags look here.
I only want the horizontal rule between each form and not at the beginning or end.
How can I alter the code so that the last horizontal rule is removed and how many different ways are there to doing this?
<form method="POST">
{% csrf_token %}
{% for form in formset %}
{{ form.as_p }}
<hr>
{% endfor %}
<button type="submit">Add Family Members</button>
</form>
Here's another example:
mylist = ['Tom', 'John', 'Jack', 'Joe']
for i in mylist:
print(i)
print('----------')
How can I alter the code so that '--------' is printed between the names but not at the end?
You want to use forloop.counter, or
{% if not forloop.first and not forloop.last %}
<hr>
{% endfor %}
In a Django template the usual method is to test the forloop.first or forloop.last variables the template engine exposes during the loop.
EG
<form method="POST">
{% csrf_token %}
{% for form in formset %}
{{ form.as_p }}
{% if not forloop.last %}
<hr>
{% endif} %}
{% endfor %}
<button type="submit">Add Family Members</button>
</form>
In plain Python code I'd try to use join for the specific case of using a delimiter to combine elements of an iterable. In the general case I can't think of an alternative to maintaining state yourself, eg a boolean indicating whether this is the first iteration of the loop or using enumerate and testing the index. Or just executing once outside the loop if you're sure your iterable is non-empty and then looping.
join returns a single string, so it's suitable when you want to pass around the joined value as well as when you want to use it for output. To replicate your exact example you'd want to include a newline in your join string. Something like:
print('----------\n'.join(mylist))
But it's often useful outside prints as well.
An explicit loop would look something like:
first = true
for element in mylist:
if not first:
print('----------')
else:
first = false
print element
Or using enumerate:
for index, element in enumerate(mylist):
if index:
print('----------')
print element
That uses the fact that a numeric type evaluates as false when it equals 0 - if index != 0: or if index > 0: is logically equivalent here, since we know index will always be a non-None and non-negative integer.
Consider I have the below multi-dimensional list which I would like to display in Flask HTML Page.
Sample List
list_html = [['Mun--CEO--Bank', 'Chee--CEO--Trust'], ['Miloš--Researcher--College'], ['Mun--CEO--Bank']]
I am trying to display this list in Flask Webpage. Where each item in list is displayed in the next line (please refer the expected output)
Sample Code
{% for dat in list_html %}
<div><span></span><ul><li> {{data[dat]}} </li></ul></div>
{% endfor %}
Here, the data refers to the list_html which I pass it in render_template.
I am unable to display each item as a new line in the output HTML page.
Expected Output
* Mun--CEO--Bank
* Chee-CEO-Trust
* Miloš--Researcher--College
* Mun--CEO--Bank
You just need a nested for loop. Something like this:
{% for dat in list_html %}
<div>
<span></span>
<ul>
{% for d in dat %}
<li> {{ d }} </li>
{% endfor %}
</ul>
</div>
<br>
{% endfor %}
Hope this helps. Good luck.
I'm using a list to populate / generate some html code dynamically as shown below,
<ul>
{% if results %}
{% for result in results %}
<li><a href="/accounts/evalpage/" ><strong>{{result.0}}</strong>{{result.1}}</a></li>
{% endfor %}
{% else %}
<li><strong>No Evaluations to Perform</strong></li>
{% endif %}
</ul>
I'm running into an issue with that if one of the list items is clicked, i need to be able to retrieve the information stored in that list item, e.g if item one is clicked i need to retrieve {{result.0}} and {{result.1}} is there a way i can retrieve this information?
List is shown below :
[['User1', 'InProgress'], ['User2'], ['User3'], ['User3'], ['User4']]
For instance, if the row containing User1 and InProgress is clicked by the end user, i want to be able to have the information User1 and InProgress within Django such that operations can be performed with them
It should be clear that in order for the backend to know what object you clicked on, you need to pass that value in the URL. So, you'll need to change the definition of your "evalpage" URL to accept a parameter:
url(r'^accounts/evalpage/(?P<user>\w+)/$', views.evalpage, name='evalpage')
and the view signature:
def evalpage(request, user):
...
and now you can do:
...
try this:
<ul>
{% if results %}
{% for result in results %}
<li>
<a href="/accounts/evalpage/" >
{% for item in result %}
<strong>{{item}}</strong>
{% endfor %}
</a>
</li>
{% endfor %}
{% else %}
<li><strong>No Evaluations to Perform</strong></li>
{% endif %}
</ul>
From my limited experience in Django so far i've only wanted to use Strings or Integers for a kwarg, is it possible to supply multiple values to 1 kwarg through a list or tuple?
Example edit**
I'm building a DetailView that has the ability to be filtered like so:
<ul class="list--inline--buttons left">
<li>
Role Line <i class="fa fa-caret-down"></i>
<ul id="role-line" class="f-dropdown" data-dropdown-content>
{% with 'all att mid def gk' as role_lines %}
{% for rl in role_lines.split %}
{% cycle 'All' 'ATT' 'MID' 'DEF' 'GK' as role_lines_name silent %}
<li>
<a href="{% url url_string object.slug card_type|default:'all' rl sort_by|default:'ovr' %}">
{{ role_lines_name }}
</a>
</li>
{% endfor %}
{% endwith %}
</ul>
</li>
{% if role_line %}
<li class="active">{{ role_line|capfirst }}</li>
{% endif %}
</ul>
Which builds /all/att/ovr/
So they can either filter for 1 role_line at a time not multiple role_lines. So i'd ideally like for the to be able to choose att then if they chose mid it would just append it to the role_line kwarg to create /all/att/mid/ovr/
Well I do not know why you are doing it this way but the template is not the place for things like this. you can just add this to the context from you view as a list
role_lines = ['all','att','mid','def','gk']
context.update({'role_lines':role_lines})
Now you can do as you like with it in the template,you do not need the
with
you do not need the split too. if you need it in any format(title,lower,etc) do it in your view and pass it to the context from the view. if you need it in every template put it in a function and add it to the settings
TEMPLATE_CONTEXT_PROCESSORS
tuple and thats it.