Making a web app that lets a user log workouts. I have a page that groups all the logged workouts together by date, using regroup and a for 2 loops. How do I only show the user that is logged in information their own information. When I try to use a 3rd for loop (ex. {% for workout in workouts%}, {% if workout.person == user %})I just get 2 tables with the same information. Heres the code in my template.
{% regroup workouts by date_of as dateof %}
{%for date_of in dateof %}
<table class="table table-striped table-dark mt-3">
<thead>
<th scope="col">{{date_of.grouper}}</th>
</thead>
<thead>
<tr>
<th scope="col">Body Part</th>
<th scope="col">Exercise</th>
<th scope="col">Weight(lbs)</th>
<th scope="col">Sets</th>
<th scope="col">Reps</th>
</tr>
</thead>
<tbody>
{% for workout in date_of.list %}
<tr>
<td>{{workout.body_part}}</td>
<td>{{workout.exercise}}</td>
<td>{{workout.weight}}</td>
<td>{{workout.sets}}</td>
<td>{{workout.reps}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
Thanks #gunsodo, after looking into it filtering the ListView with get_queryset solved the problem. 🙌🏼
Related
I am listing the rows of a table (fields: Name, Emails, Category, Expiry) via a template:
<table border="1" style="width:100%">
<tr>
<th style="width:20%">Name</th>
<th style="width:40%">Emails</th>
<th>Category</th>
<th>Expiry </th>
<th>Action</th>
</tr>
{% for obj in expiredlist %}
<tr>
<td>{{obj.name}}</td>
<td>{{obj.emails}}</td>
<td>{{obj.category}}</td>
<td>{{obj.expiry}}</td>
</tr>
{% endfor %}
</table>
Now I want to allow users to edit one of the fields on the fly (like how we do it in datatable).
I tried to add a new column by adding:
<td><form method="post">
{{form.expiry}}
</form>
<input type="submit" value="Submit"></td>
Thinking that the above will allow users to edit expiry field on the fly. I know I am not passing any info about each row (not able to connect with id).
Can someone guide me how can I do this?
I have a model for coffee beans that I send over to my HTML pages and display the objects' data. Currently, I have a solution that requires me to update my HTML code if I modify my Bean object.
I want to the remove coupling from my program by iterating over the attributes associated with the object as opposed to hard-coding each attribute into the HTML.
My current (outdated) solution:
# Bean model defined using Flask-SQLAlchemy
class Bean(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), index=True, nullable=False)
...
<!-- some_page.html -->
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
...
</tr>
</thead>
<tbody>
{% for bean in beans %}
{% include '_bean_info.html' %}
{% endfor %}
</tbody>
</table>
<!-- _bean_info.html -->
<tr>
<th scope="col">{{ bean.id }}</th>
<th scope="col">{{ bean.name }}</th>
...
</tr>
What I am trying to do:
<!-- _bean_info.html -->
<tr>
{% for each b in bean %}
<th scope="col">
{{ bean.b }} <!-- where 'b' is an attribute, e.g. 'name' -->
</th>
</tr>
Depending on what your data structure looks like, you may be able to take advantage of the fact that the attributes of an object are available via the __dict__ attribute...in other words, if we have data like this:
from dataclasses import dataclass
#dataclass
class Bean:
id: str
name: str
beans = [
Bean(id=1, name="Guatemalan"),
Bean(id=2, name="Peruvian"),
Bean(id=3, name="Hawaiian"),
]
Then we can use that in a template like this:
import jinja2
t = jinja2.Template('''
<table>
{% for bean in beans %}
<tr>
{% for attr in bean.__dict__.keys() -%}
<td>{{ attr }}</td><td>{{ bean[attr] }}</td>
{% endfor -%}
</tr>
{% endfor %}
</table>
''')
print(t.render(beans=beans))
Put together, the examples above will produce:
<table>
<tr>
<td>id</td><td>1</td>
<td>name</td><td>Guatemalan</td>
</tr>
<tr>
<td>id</td><td>2</td>
<td>name</td><td>Peruvian</td>
</tr>
<tr>
<td>id</td><td>3</td>
<td>name</td><td>Hawaiian</td>
</tr>
</table>
...which is I think what you want.
In this particular situation, I would probably explicitly convert my model into a dict, because this makes the template a little less magical:
from dataclasses import dataclass, asdict
...
t = jinja2.Template('''
<table>
{% for bean in beans %}
<tr>
{% for attr in bean.keys() -%}
<td>{{ attr }}</td><td>{{ bean[attr] }}</td>
{% endfor -%}
</tr>
{% endfor %}
</table>
''')
print(t.render(beans=(asdict(bean) for bean in beans)))
I'm trying to add a delete button on my users admin page in a flask app.
But I already have this error when I click on button :
sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.str' is not mapped
This is my adminusers.html file :
{% extends 'base.html' %}
{% block main %}
<main>
<table class="table table-striped users">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Roles</th>
<th scope="col" class="text-center">Delete</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<th scope="row">{{ user.id }}</th>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>
{% for role in user.roles %}
{{role.name}};
{% endfor %}
</td>
<td style="text-align: center;"></i></td>
</tr>
{% endfor %}
</tbody>
</table>
</main>
{% endblock main %}
And my app.py file :
#app.route('/adminusers')
def list_users():
users= User.query.all()
return render_template('adminusers.html', users=users)
#app.route('/delete_user/<user>')
def delete_user(user):
user_datastore.delete_user(user=user)
db.session.commit()
return redirect(url_for('adminusers'))
I'm trying to use the 'email' or 'name' but it already return an error
Thanks #pjcunningham, like that it works :
#app.route('/delete_user/<id>')
def delete_user(id):
user = user_datastore.get_user(id)
user_datastore.delete_user(user)
db.session.commit()
return redirect(url_for('adminusers'))
As I iterate results. There are rows that can be merged, however, relative columns will have multiple rows.
For example:
Currently:
Domain ID----sub Domain Title---- Sub domain ID
A1-----------------A1 title-----------A1.1
A1-----------------A1 title-----------A1.2
I would like it to be like this. With first two columns with merged rows
Domain ID----sub Domain Title---- Sub domain ID
A1----------------A1 title------------A1.1
---------------------------------------- A1.2
<table class="table table-sm">
<tr>
<th>Domain ID</th>
<th>Sub Domain title</th>
<th>Sub domain ID</th>
</tr>
{% for record in a6titles %}
<tr>
<td>A6</td>
<td>A6 title</td>
<td>titles: {{ record }}</td>
</tr>
{% endfor %}
</table>
Try this.
<table class="table table-sm">
<tr>
<th>Domain ID</th>
<th>Sub Domain title</th>
<th>Sub domain ID</th>
</tr>
{% for record in a6titles %}
<tr>
<td colspan=3>A6</td>
<td>titles: {{ record }}</td>
</tr>
{% endfor %}
</table>
my table looks like
date name place
4.09.2018 jack London
date name place
4.09.2018 ed paris
date name place
5.09.2018 sam istabul
i have problem with using table. i want to show one table my data but , it doesnt work, my program showing all my data different table but i dont want this. how i repair this situation ?
i want to show one header and all my data below from header like this
- date name place
4.09.2018 jack London
4.09.2018 ed paris
5.09.2018 sam istabul
but this situation adding header for all of my data. it shows here
my html codes
thank u for your interest...
<div class="container">
<div class="row">
<div class="col-3">
<a class="btn btn-warning" href="{% url 'ANASAYFA'%}"> ANASAYFA </a>
<br>
{% now "jS F Y H:i" %}
</div>
<div class="col-6">
{% for i in veriler %}
<table class="table table-bordered table-dark">
<thead>
<tr>
<th scope="col">TARİH</th>
<th scope="col">İSİM-SOYİSİM</th>
<th scope="col">VARDİYA BÖLGE</th>
<th scope="col">VARDİYA DÖNEM</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="i.row">{{ i.gun }}</th>
<th>{{ i.personel }}</th>
<th>{{ i.bolge }}</th>
<td>{{ i.vardiya_donemi }}</td>
</tr>
</tbody>
</table>
{% endfor %}
Try this:
<table class="table table-bordered table-dark">
<thead>
<tr>
<th scope="col">TARİH</th>
<th scope="col">İSİM-SOYİSİM</th>
<th scope="col">VARDİYA BÖLGE</th>
<th scope="col">VARDİYA DÖNEM</th>
</tr>
</thead>
<tbody>
{% for i in veriler %}
<tr>
<th scope="i.row">{{ i.gun }}</th>
<th>{{ i.personel }}</th>
<th>{{ i.bolge }}</th>
<td>{{ i.vardiya_donemi }}</td>
</tr>
{% endfor %}
</tbody>
</table>