I am using flask, sqlalchemy, jinja2 and python and I am struggling after getting data from the database and displaying them in my html while they are inside a list and in an anchor to post them with anchor click inside a form to submit them afterwords in another database table. Here is what I have so far.
My html
<div class="container-fluid">
<div id="training_list">
<h1>List of Exercises</h1>
{% for exercises in exlist %}
<li id="li_ex_list"><a href="#">{{ exercises.name }}</a> | Reputations:
{{ exercises.reps }}</li>
{% endfor %}
</div>
<div id="training_content">
<h1>My Workout Plan</h1>
<form method="POST" action="/my_workouts">
</form>
</br>
<button type="submit" class="btn btn-info">Save my plan</button>
</div>
</div>
My flask-python
class Exercises(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40), unique=True, nullable=False)
reps = db.Column(db.Integer(), nullable=False)
complete = db.Column(db.Boolean, default=False)
def __init__(self, name, reps, complete):
self.name = name
self.reps = reps
self.complete = complete
def __repr__(self):
return '<Exercises %r>' % self.name
class Traningplan(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
ex_id = db.Column(db.Integer, db.ForeignKey('exercises.id'), nullable=False)
ex_name = db.Column(db.String(40), db.ForeignKey('exercises.name'), nullable=False)
class Usertrainingplan(FlaskForm):
user_id = IntegerField('Userid', render_kw={'readonly': True})
ex_id = IntegerField('Exerciseid', render_kw={'readonly': True})
ex_name = StringField('Exercisename', render_kw={'readonly': True})
@app.route('/my_workouts', methods=['GET', 'POST'])
@login_required
def my_workouts():
exlist = Exercises.query.all()
form = Usertrainingplan()
return render_template('My_Training_Programs.html', exlist=exlist, form=form)
I have tried filtering for complete True/False and passing 2 variables for complete and incomplete(also added them with jinja2 template in my html) but I can't make it work. ---- This part was inside "def_myworkouts():"-----
incomplete = Exercises.query.filter_by(complete=False).all()
complete = Exercises.query.filter_by(complete=True).all()
@app.route('/complete/<id>')
def complete(id):
exercises = Exercises.query.filter_by(id=int(id)).first()
exercises.complete = True
db.session.commit()
return redirect(url_for('my_workouts'))
Additional Information: Current page looks like this: current page
所以我想做的是:
一旦用户点击左侧的链接,我希望链接的文本(例如Landmine)添加到页面右侧“我的锻炼计划”所在的位置,并创建一个包含所有链接的列表所选项目。 另外,我希望能够将它们放在表单中,并将表单提交(POST)到我的数据库中的表中,其中列为:user_id,ex_id,ex_name作为外键。 user_id(外键)应该是当前登录的用户id(主键),ex_id(外键)应该是练习的id,最后ex_name(外键)应该是练习的名称。课程的主键已经在所需表格中的数据库中提交,同样的练习名称也是如此。
感谢您提前的时间。任何高度适用的帮助。
Stackoverflow你是我唯一的救世主