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.
Related
In this html block:
{% block body %}
<h1>All Pages</h1>
<ul>
{% for x in entries %}
{% comment %} {% wik = 'mask/' + x + '.md' %} {% endcomment %}
<li><a href = 'mask/'{{x}} </a>{{ x }}</li>
{% endfor %}
</ul>
{% endblock %}
I am trying to insert a string in x that will go to end of the hypertext. mask/cat, where "cat" is x [just as an example]. When I do this, the entry does not seem to be added to the hyperlink when I click it.
The other way I thought is to create a python variable wik with the correct page hyperlink, but then I get an issue in Django
Invalid block tag on line 12: 'wik', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
Any help appreciated
thanks
Your problem here is that your variable is outside of the href, this should work:
<li><a href = 'mask/{{x}}' </a>{{ x }}</li>
If it's a variable based url on your app though, I would consider using a url pattern for a cleaner solution
{% for item in dlist %}
<p>{{ item }}</p>
{% endfor %}
{% for i in range(dlist|length) %}
{{ dlist.pop(0) }}
{% endfor %}
After using the list "dlist", I want to clear the data on it it so that upon next iteration the list starts from size 0. But if I do it this way upon using dlist.pop(0), the data is being printed on my html page and I don't want that. How can I do that?
If you want to clear the list completely, you can clear the list using clear() method. Details of the method can be read from the official documentation.
The {{}} delimiter will display the result even if it is None value. We need to block it as we want to clear the list only and we do not want to show None after executing it. I have added a condition inside the delimiter to hide the None from output.
app.py:
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/", methods=["GET"])
def home():
dlist = ["Ahmedur", "Rahman", "Shovon", "arsho"]
return render_template("data.html", dlist=dlist)
data.html:
{% for item in dlist %}
<p>{{ item }}</p>
{% endfor %}
{{ dlist.clear() if dlist.clear()}}
<hr>
{% for item in dlist %}
<p>{{ item }}</p>
{% endfor %}
Output:
The second for loop after the hr tag is not showing any value in the output. Because dlist doesn't contain any value after the execution of clear() method.
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 %}.
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>
I am working on a small web app to view some logfiles. But the
queries I issue to the database use to get very big.
I wanted to implement some pagination following this example pagination.
I put the class into a single file which gets loaded in the Flask view. Next I implemented
my pagination view like this:
#app.route('/index/', defaults={'page':1})
#app.route('/index/page/<int:page>')
def index(page):
count = db_session.execute("select host,facility,level,msg from messages").rowcount
tblqry = db_session.execute("select host,facility,level,msg from messages").fetchmany(size=1000)
if not tblqry and page != 1:
abort(404)
pagination = Pagination(page, PER_PAGE, count)
return render_template('index.html', pagination=pagination, tblqry=tblqry)
after that I created a macro file named _pagination_helper.html with the macro contents from the macro example. Then I imported the pagination_helper macro with:
{% from "_pagination_helper.html" import render_pagination %}
but when I then try to do something like this:
{{ render_pagination(host[0]) }}
flask claims:
UndefinedError: 'str object' has no attribute 'iter_pages'
so why does flask fails to find the 'iter_pages' because I included the pagination class in the views file?
And also I am not really sure where to put the URL Generation Helper from the How To.
Edit:
This is what my pagination_helper looks like:
{% macro render_pagination(pagination) %}
<div class=pagination>
{% for page in pagination.iter_pages() %}
{% if page %}
{% if page != pagination.page %}
{{ page }}
{% else %}
<strong>{{ page }}</strong>
{% endif %}
{% else %}
<span class=ellipsis>…</span>
{% endif %}
{%- endfor %}
{% if pagination.has_next %}
Next »
{% endif %}
</div>
{% endmacro %}
You are expected to pass a Pagination object to the macro, not a string. host[0] is a string, not the pagination value you created in your view function.
Use:
{{ render_pagination(pagination) }}