Answer the question
In order to leave comments, you need to log in
How to automatically fill Flask-WTF forms?
I have a database with links
I have a user profile in which I want to display in a table all the links associated with this user
I want the user to be able to change some columns of the database row through this table
I do it like this:
profile.html :
{% extends 'base.html' %}
{% block content %}
<ul class"userdata">
<li>Username: {{ current_user.username }}</li>
<li>Email: {{ current_user.email }}</li>
<li><a href="#">Change password</a></li>
</ul>
<h3>Your links list</h3>
<table border="1">
<tr>
<th>shortlink</th>
<th>stats</th>
<th>privat</th>
</tr>
{% for link in current_user.links %}
<tr>
<form class="" method="post">
{{ link_form.hidden_tag() }}
<th>{{ link_form.shortlink() }}</th>
<th>{{ link.count }}</th>
<th>{{ link_form.privat_stats() }}</th>
<th>{{ link_form.save() }}</th>
</form>
</tr>
{% endfor %}
</table>
{% endblock %}
from werkzeug.security import check_password_hash
from app import db, login_manager
from flask_login import UserMixin
@login_manager.user_loader
def load_user(user_id):
return Users.query.get(user_id)
class Users(db.Model, UserMixin):
__tablename__ = "Users"
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(64), unique = True, index = True)
email = db.Column(db.String(64), unique = True, index = True)
password = db.Column(db.String(120))
links = db.relationship('Links', back_populates = 'user') # two-side relationship with Links
def check_password(self, password):
return check_password_hash(self.password, password)
class Links(db.Model):
__tablename__ = 'Links'
id = db.Column(db.Integer, primary_key = True, unique = True)
user_id = db.Column(db.Integer, db.ForeignKey('Users.id')) # points to id in Users table
longlink = db.Column(db.Text)
shortlink = db.Column(db.Text)
privat_stats = db.Column(db.Boolean)
count = db.Column(db.Integer)
user = db.relationship('Users', back_populates = 'links', uselist = False) # two-side relationship with Users
@app.route('/profile')
@login_required
def profile():
link_form = LINKform()
if link_form.validate_on_submit():
pass
return render_template('profile.html', link_form = link_form)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question